Java OCA OCP Practice Question 212

Question

Given:

3. class MyClass {
4.   int size;/* www.  ja va2 s .c om*/
5.   MyClass(int s) { size = s; }
6. }
7. public class Main {
8.   public static void main(String[] args) {
9.     MyClass b1 = new MyClass(5);
10.     MyClass[] ba = go(b1, new MyClass(6));
11.     ba[0] = b1;
12.     for(MyClass b : ba) System.out.print(b.size + " ");
13.   }
14.   static MyClass[] go(MyClass b1, MyClass b2) {
15.      b1.size = 4;
16.      MyClass[] ma = {b2, b1};
17.      return ma;
18.   }
19. }

What is the result?

  • A. 4 4
  • B. 5 4
  • C. 6 4
  • D. 4 5
  • E. 5 5
  • F. Compilation fails


A is correct.

Note

Although main()'s b1 is a different reference variable than go()'s b1, they refer to the same MyClass object.




PreviousNext

Related