Java OCA OCP Practice Question 787

Question

What is the result of running the following program?

1:   package mypkg; 
2:   public class Main { 
3:      static int[][] v; 
4: //from   ww w . ja  v  a  2s.c  o  m
5:      public static void main(String args[]) { 
6:         v[3][3] = 6; 
7:         Object[] obj = v; 
8:         obj[3] = 'X'; 
9:         System.out.println(v[3][3]); 
10:     } 
11:  } 
  • A. 6
  • B. X
  • C. The code does not compile.
  • D. The code compiles but throws a NullPointerException at runtime.
  • E. The code compiles but throws a different exception at runtime.
  • F. The output is not guaranteed.


D.

Note

The instance and class variables are initialized to null.

Line 6 throws a NullPointerException.

If the array was declared, the answer would be E because the code would throw an ArrayStoreException on line 8.




PreviousNext

Related