Java OCA OCP Practice Question 2693

Question

Given:

2. public class Main {  
3.   public static void main(String[] args) {  
4.     boolean earlyExit = new Main().test1(args);  
5.     if(earlyExit) 
             assert false;  
6.     new Main().test2(args);  
7.   }  /* w  ww .java 2  s .co  m*/
8.   boolean test1(String[] a) {   
9.     if (a.length == 0) 
            return false;  
10.       return true;  
11.  }  
12.  private void test2(String[] a) {  
13.     if (a.length == 2) assert false;  
14.  } 
15.} 

Which are true? (Choose all that apply.)

  • A. Compilation fails.
  • B. The assertion on line 5 is appropriate.
  • C. The assertion on line 13 is appropriate.
  • D. "java -ea Main" will run without error.
  • E. "java -ea Main x" will run without error.
  • F. "java -ea Main x y" will run without error.
  • G. "java -ea Main x y z" will run without error.


D is correct.

Note

This invocation will run without error.

A, B, and C are incorrect because the code compiles, and neither assertion is appropriate because ultimately they are testing the args of main().

E, F, and G are incorrect.

Each will throw an AssertionError.




PreviousNext

Related