Java OCA OCP Practice Question 2929

Question

Given:

public class Main {
   final static int mine = 7;
   final static Integer i = 57;

   public static void main(String[] args) {
      int x = go(mine);
      System.out.print(mine + " " + x + " ");
      x += mine;/*w w  w  . j  a  va 2 s  .co  m*/
      Integer i2 = i;
      i2 = go(i);
      System.out.println(x + " " + i2);
      i2 = new Integer(60);
   }

   static int go(int x) {
      return ++x;
   }
}

What is the result?

  • A. 7 7 14 57
  • B. 7 8 14 57
  • C. 7 8 15 57
  • D. 7 8 15 58
  • E. 7 8 16 58
  • F. Compilation fails.
  • G. An exception is thrown at runtime.


D is correct.

Note

None of the final variables are modified.

The "i2" variable initially refers to the same object as the final "i" variable, but that object can be modified, and "i2" can be referred to a different object.

Autoboxing allows an Integer to work with go()'s int argument.




PreviousNext

Related