What will be the output of the following code? public class ExceptionDemo {
What will be the output of the following code?
public class ExceptionDemo {
public static void main(String[] args) {
try {
int[] numbers = {1, 2, 3};
System.out.println(numbers[5]);
} catch (Exception e) {
System.out.println("Exception caught: " + e.getClass().getName());
} finally {
System.out.println("Execution completed.");
}
}
}
Show answerHide answer
Correct answer: (B) Exception caught: java.lang.ArrayIndexOutOfBoundsException. Execution completed.
Explanation
numbers[5] is past the end of the array, so an ArrayIndexOutOfBoundsException is thrown. getClass().getName() gives its full name, and finally always runs.