Java OCA OCP Practice Question 2815

Question

Given

1. public class Main {  
2.   Main main;  /*w  w w.  j a v  a2 s .  c om*/
3.   String count = "0";  
4.   public static void main(String[] args) {  
5.     Main k1 = new Main();  
6.     go(k1);  
7.     System.out.println(k1.count);  
8.   }  
9.   static Main go(Main k) {  
10.     final Main k1 = new Main();  
11.     k.main = k1;  
12.     k.main.count = "7";  
13.     return k.main;  
14.  } 
15.} 

What is the result?

  • A. 0
  • B. 7
  • C. null
  • D. Compilation fails.
  • E. An exception is thrown at runtime.


A is correct.

Note

The Main object referred to by k1 has a Main object, and that object's count variable is updated, but the original Main object's count variable is not updated.




PreviousNext

Related