Java OCA OCP Practice Question 158

Question

Given:

class Main { /* w  w  w  . j  av  a2 s .c  om*/
  int x = 5; 
  public static void main(String[] args) { 
    final Main f1 = new Main(); 
    Main f2 = new Main(); 
    Main f3 = MainSwitch(f1,f2); 
    System.out.println((f1 == f3) + " " + (f1.x == f3.x)); 
  } 
  static Main MainSwitch(Main x, Main y) { 
    final Main z = x; 
    z.x = 6; 
    return z; 
} } 

What is the result?

  • A. true true
  • B. false true
  • C. true false
  • D. false false
  • E. Compilation fails
  • F. An exception is thrown at runtime


A is correct.

Note

The references f1, z, and f3 all refer to the same instance of Main.

The final modifier assures that a reference variable cannot be referred to a different object, but final doesn't keep the object's state from changing.




PreviousNext

Related