Java OCA OCP Practice Question 160

Question

Given:

public class Main { 
  int size = 7; //from  w ww.j ava 2  s.  c  om
  public static void main(String[] args) { 
    Main m1 = new Main(); 
    Main m2 = m1; 
    int i1 = 10; 
    int i2 = i1; 
    go(m2, i2); 
    System.out.println(m1.size + " " + i1); 
  } 
  static void go(Main m, int i) { 
    m.size = 8; 
    i = 12; 
  } 
} 

What is the result?

  • A. 7 10
  • B. 8 10
  • C. 7 12
  • D. 8 12
  • E. Compilation fails
  • F. An exception is thrown at runtime


B is correct.

Note

In the go() method, m refers to the single Main instance, but the int i is a new int variable, a detached copy of i2.




PreviousNext

Related