Practice mode Java

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.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.