closepop
closepop

Would you like to submit the test?


Java :

Assertions




1.
What will be the output of the program?

public class Test 
{  
    public static void main(String[] args) 
    { 
        int x = 0;  
        assert (x > 0) ? "assertion failed" : "assertion passed" ; 
        System.out.println("finished");  
    } 
}

finished
compilation fails
An AssertionError is thrown and finished is output.
An AssertionError is thrown with the message "assertion failed."




2.
public class Test 
    public void foo() 
    {
        assert false; /* Line 5 */
        assert false; /* Line 6 */
    } 
    public void bar()
    {
        while(true)
        {
            assert false; /* Line 12 */
        } 
        assert false;  /* Line 14 */
    } 
}
What causes compilation to fail?

Line 5
Line 12
Line 8
Line 14




3.
What will be the output of the program?

public class Test 
{
    public static int y;
    public static void foo(int x) 
    {
        System.out.print("foo ");
        y = x;
    }
    public static int bar(int z) 
    {
        System.out.print("bar ");
        return y = z;
    }
    public static void main(String [] args ) 
    {
        int t = 0;
        assert t > 0 : bar(7);
        assert t > 1 : foo(8); /* Line 18 */
        System.out.println("done ");
    }
}

bar
bar done
foo done
compilation fails




4.
What will be the output of the program (when you run with the -ea option) ?

public class Test 
{  
    public static void main(String[] args) 
    {
        int x = 0;  
        assert (x > 0) : "assertion failed"; /* Line 6 */
        System.out.println("finished"); 
    } 
}

finished
Compilation fails.
An AssertionError is thrown.
An AssertionError is thrown and finished is output.




5.
public class Test2 
{
    public static int x;
    public static int foo(int y) 
    {
        return y * 2;
    }
    public static void main(String [] args) 
    {
        int z = 5;
        assert z > 0; /* Line 11 */
        assert z > 2: foo(z); /* Line 12 */
        if ( z < 7 )
            assert z > 4; /* Line 14 */

        switch (z) 
        {
            case 4: System.out.println("4 ");
            case 5: System.out.println("5 ");
            default: assert z < 10;
        }

        if ( z < 10 )
            assert z > 4: z++; /* Line 22 */
        System.out.println(z);
    }
}
which line is an example of an inappropriate use of assertions?

Line 14
Line 12
Line 11
Line 22




6.
Which statement is true?

Assertions can be enabled or disabled on a class-by-class basis.
Conditional compilation is used to allow tested classes to run at full speed
Assertions are appropriate for checking the validity of arguments in a method.
The programmer can choose to execute a return statement or to throw an exception if an assertion fails.




7.
Which three statements are true?

Assertion checking is typically enabled when a program is deployed.
It is never appropriate to write code to handle failure of an assert statement.
Assertion checking is typically enabled during program development and testing.
Assertion checking can be selectively enabled or disabled on a per-package basis, but not on a per-class basis.
Assertion checking can be selectively enabled or disabled on both a per-package basis and a per-class basis.

1,2,4
2,3,5
2,5,3
2,1,3




8.
public class Test 
    public void foo() 
    {
        assert false; /* Line 5 */
        assert false; /* Line 6 */
    } 
    public void bar()
    {
        while(true)
        {
            assert false; /* Line 12 */
        } 
        assert false;  /* Line 14 */
    } 
}
What causes compilation to fail?

Line12
Line13
Line 14
Line 10




9.
public class Test2 
{
    public static int x;
    public static int foo(int y) 
    {
        return y * 2;
    }
    public static void main(String [] args) 
    {
        int z = 5;
        assert z > 0; /* Line 11 */
        assert z > 2: foo(z); /* Line 12 */
        if ( z < 7 )
            assert z > 4; /* Line 14 */

        switch (z) 
        {
            case 4: System.out.println("4 ");
            case 5: System.out.println("5 ");
            default: assert z < 10;
        }

        if ( z < 10 )
            assert z > 4: z++; /* Line 22 */
        System.out.println(z);
    }
}
which line is an example of an inappropriate use of assertions?

Line 23
Line 22
Line 25
Line 20




10.
Which of the following statements is true?

It is sometimes good practice to throw an AssertionError explicitly.
Private getter() and setter() methods should not use assertions to verify arguments
If an AssertionError is thrown in a try-catch block, the finally block will be bypassed.
It is proper to handle assertion statement failures using a catch (AssertionException ae) block.










FreshersLive - No.1 Job site in India. Here you can find latest 2016 government as well as private job recruitment notifications for different posts vacancies in India. Get top company jobs for both fresher and experienced. Job Seekers can get useful interview tips, resume services & interview Question and answer. Practice online test free which is helpful for interview preparation. Register with us to get latest employment news/rojgar samachar notifications. Also get latest free govt and other sarkari naukri job alerts daily through E-mail.


FreshersLive - No.1 Job site in India. Here you can find latest 2016 government as well as private job recruitment notifications for different posts vacancies in India. Get top company jobs for both fresher and experienced. Job Seekers can get useful interview tips, resume services & interview Question and answer. Practice online test free which is helpful for interview preparation. Register with us to get latest employment news/rojgar samachar notifications. Also get latest free govt and other sarkari naukri job alerts daily through E-mail.