Java OCA OCP Practice Question 1525

Question

What will happen when the following code is compiled and run?

public class Main {
   static int[] x = new int[0];
   static {/*  w w  w.  j  a va  2  s . com*/
      x[0] = 10;
   }

   public static void main(String[] args) {
      Main ax = new Main();
   }
}

Select 1 option

A. It will throw NullPointerException at runtime.
B. It will throw ArrayIndexOutOfBoundsException at runtime.
C. It will throw ExceptionInInitializerError at runtime.
D. It will not compile.


Correct Option is  : C

Note

Even though the line x [0] = 10; will throw java.lang.ArrayIndexOutOfBoundsException,

JVM will wrap it and rethrow java.lang.ExceptionInInitializerError.




PreviousNext

Related