Q.1

Which of the following statements is true?

  • If assertions are compiled into a source file, and if no flags are included at runtime, assertions will execute by default.
  • As of Java version 1.4, assertion statements are compiled by default.
  • With the proper use of runtime arguments, it is possible to instruct the VM to disable assertions for a certain class, and to enable assertions for a certain package, at the same time.
  • When evaluating command-line arguments, the VM gives -ea flags precedence over -da flags.
Q.2

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.
  • 1, 2 and 4
  • 2, 3 and 5
  • 3, 4 and 5
  • 1, 2 and 5
Q.3

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.
Q.4

Which statement is true about assertions in the Java programming language?

  • Assertion expressions should not contain side effects.
  • Assertion expression values can be any primitive type.
  • Assertions should be used for enforcing preconditions on public methods.
  • An AssertionError thrown as a result of a failed assertion should always be handled by the enclosing method.
Q.5

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.
Q.6

Which of the following statements is true?

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

public class Test{
    public static int x;
    public static int foo(int y) 
    {
        return y *    }
    public static void main(String [] args) 
    {
        int z =        assert z >/* Line*/
        assert z >foo(z); /* Line*/
        if ( z < 7 )
            assert z >/* Line*/

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

        if ( z <)
            assert z >z++; /* Line*/
        System.out.println(z);
    }
}
which line is an example of an inappropriate use of assertions?
  • Line 11
  • Line 12
  • Line 14
  • Line 22
Q.8

What will be the output of the program?

public class Test 
{  
    public static void main(String[] args) 
    { 
        int x = 
        assert (x >? "assertion failed" : "assertion passed" ; 
        System.out.println("finished");  
    } 
}
  • finished
  • Compiliation fails.
  • An AssertionError is thrown and finished is output.
  • An AssertionError is thrown with the message "assertion failed."
Q.9

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

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

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 =        assert t > 0 : bar(7);
        assert t > 1 : foo(8); /* Line*/
        System.out.println("done ");
    }
}
  • bar
  • bar done
  • foo done
  • Compilation fails
Q.12

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 = 
        assert (x >: "assertion failed"; /* Line 6 */
        System.out.println("finished"); 
    } 
}
  • finished
  • Compilation fails.
  • An AssertionError is thrown.
  • An AssertionError is thrown and finished is output.
Q.13

public class Test{
    public static int x;
    public static int foo(int y) 
    {
        return y *    }
    public static void main(String [] args) 
    {
        int z =        assert z >/* Line*/
        assert z >foo(z); /* Line*/
        if ( z < 7 )
            assert z >/* Line*/

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

        if ( z <)
            assert z >z++; /* Line*/
        System.out.println(z);
    }
}
which line is an example of an inappropriate use of assertions?
  • Line 11
  • Line 12
  • Line 14
  • Line 22
0 h : 0 m : 1 s