Java OCA OCP Practice Question 94

Question

What does the following code print?

public class A { 
     static int x; 

     public static void main(String[] args) { 
           A that1 = new A(); 
           A that2 = new A(); 
           that1.x = 5; //from   w  w  w  . j  a  v a  2s  . c  om
           that2.x = 1000;  
           x = -1;
           System.out.println(x);
     }
}
  • A. 0
  • B. 5
  • C. 1000
  • D. -1


D.

Note

Since x is static, there is only one variable, which is shared by all instances along with the class.

Thus the last assignment wins.




PreviousNext

Related