Java OCA OCP Practice Question 902

Question

What is the result of running the following program?

2:   public class MyClass { 
3:      static int[][] game = new int[6][6]; 
4: /*w w w  .  j  a  v  a2 s. co  m*/
5:      public static void main(String[] args) { 
6:         game[3][3] = 6; 
7:         Object[] obj = game; 
8:         obj[3] = "X"; 
9:         System.out.println(game[3][3]); 
10:     } 
11:  } 
  • A. X
  • B. The code does not compile.
  • C. The code compiles but throws a NullPointerException at runtime.
  • D. The code compiles but throws a different exception at runtime.


D.

Note

Line 6 assigns an int to a cell in a 2D array.

This is fine.

Line 7 casts to a general Object[].

This is dangerous, but legal.

That brings us to line 8.

The compiler can't protect us from assigning a String to the int[] because the reference is more generic.

Line 8 throws an ArrayStoreException because the type is incorrect, and Option D is correct.

You couldn't have assigned an int on line 8 either because obj[3] is really an int[] behind the scenes and not an int.




PreviousNext

Related