Java OCA OCP Practice Question 2704

Question

Given:

3. public class Main {  
4.   public static void main(String[] args) {  
5.      int i = 4;  
6.      int j = 1;  
7.  /*from  w  ww  .ja  va 2  s  . c o m*/
8.      assert(i > Integer.valueOf(args[0]));  
9.      assert(j > Integer.valueOf(args[0])): "error 1";  
10.     assert(j > i): "error 2": "passed";  
11.  } 
12.} 

And, if the code compiles, given the following two command-line invocations:

  • I. java -ea Main 2
  • II. java -ea Main 0

Which are true? (Choose all that apply.)

  • A. Compilation fails.
  • B. Invocations I and II will throw an AssertionError that will add String data to the program's execution log.
  • C. Invocations I and II will throw an AssertionError that will add String data to the program's stack trace.
  • D. Not all of the assert statements use assertions appropriately.


A and D are correct.

Note

The syntax on line 10 is invalid and will not compile.

The assert statements on lines 8 and 9 are testing the arguments of a public method, which is considered an inappropriate use of assertions.

If the syntax on line 10 was fixed, then option C would be correct.




PreviousNext

Related