Assertions

1
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?

A. Line 5
B. Line 6
C. Line 12
D. Line 14

2
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?

A. Line 11
B. Line 12
C. Line 14
D. Line 22

3

Which three statements are true?

      1. Assertion checking is typically enabled when a program is deployed.

      2. It is never appropriate to write code to handle failure of an assert statement.

      3. Assertion checking is typically enabled during program development and testing.

      4. Assertion checking can be selectively enabled or disabled on a per-package basis, but not on a 

          per-class basis.

      5. Assertion checking can be selectively enabled or disabled on both a per-package basis and a 

         per-class basis.

A. 1, 2 and 4
B. 2, 3 and 5
C. 3, 4 and 5
D. 1, 2 and 5

4

Which of the following statements is true?

A. In an assert statement, the expression after the colon ( : ) can be any Java expression.
B. If a switch block has no default, adding an assert default is considered appropriate.
C. In an assert statement, if the expression after the colon ( : ) does not have a value, the assert's error message will be empty.
D. It is appropriate to handle assertion failures using a catch clause.

5

Which of the following statements is true?

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