What happens when the Java code int a = {1, 2, 3}; System.out.println(a); is run?
Correct answer D. An ArrayIndexOutOfBoundsException is thrown
Explanation
Valid indexes of a three-element array are 0, 1 and 2, so 3 is out of range. The JVM checks every array access at run time and throws ArrayIndexOutOfBoundsException. The compiler does not check index values, so the code builds successfully. Java never reads past the end of an array as C can.
Answer checked How we write and check questions
Discussion