Question discussion

What will be the output of the following code?

359443Medium DSSSB TGT Computer Science 0 replies 0 views
0
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.");
        }
    }
}
  • AException caught: java.lang.Exception . Execution completed.
  • BException caught: java.lang.ArrayIndexOutOfBoundsException. Execution completed.
  • CCompilation error
  • DException caught: java.lang.IndexOutOfBoundsException . Execution completed.
Correct answer: B

Explanation

numbers[5] is out of bounds, which throws ArrayIndexOutOfBoundsException. It is caught by catch (Exception) and printed, and then the finally block runs.

Discussion 0

Nobody has replied yet. Start the discussion!