Java OCA OCP Practice Question 2443

Question

Given:

 2. public class Main {  
 3.   static int count = 0;  
 4.   Main() {  // w w w . j  a  va 2 s  .c  om
 5.     while(count < 10) new Main(++count);  
 6.   }  
 7.   Main(int x) { super(); }  
 8.   public static void main(String[] args) {  
 9.     new Main();  
10.     new Main(count);  
11.     System.out.println(count++);  
12.   }  
13. } 

What is the result?

  • a. 9
  • B. 10
  • C. 11
  • d. 12
  • E. Compilation fails.
  • F. An exception is thrown at runtime.


B is correct.

Note

It's legal to invoke "new" from within a constructor, and it's legal to call super() on a class with no explicit superclass.

On the real exam, it's important to watch out for pre and post-incrementing.




PreviousNext

Related