Java OCA OCP Practice Question 166

Question

Given:

public class Main { 
  int id; // w w  w.  ja  v a  2s  .  c o  m
  Main(int i) { id = i; } 
  public static void main(String[] args) { 
    Main h1 = new Main(1); 
    Main h2 = h1.go(h1); 
    System.out.println(h2.id); 
  } 
  Main go(Main h) { 
    Main h3 = h; 
    h3.id = 2; 
    h1.id = 3; 
    return h1; 
  } 
} 

What is the result?

  • A. 1
  • B. 2
  • C. 3
  • D. Compilation fails
  • E. An exception is thrown at runtime


D is correct.

Note

Inside the go() method, h1 is out of scope.




PreviousNext

Related