Bird
0
0

What will be the output of the following code?

medium📝 Predict Output Q13 of 15
Java - Exception Handling

What will be the output of the following code?

try {
    int[] arr = new int[2];
    System.out.println(arr[5]);
} catch (ArrayIndexOutOfBoundsException e) {
    System.out.println("Index error");
} catch (Exception e) {
    System.out.println("General error");
}
ANo output
BGeneral error
CArrayIndexOutOfBoundsException
DIndex error
Step-by-Step Solution
Solution:
  1. Step 1: Identify exception thrown

    Accessing arr[5] causes ArrayIndexOutOfBoundsException.
  2. Step 2: Match catch block

    The first catch matches ArrayIndexOutOfBoundsException and prints "Index error".
  3. Final Answer:

    Index error -> Option D
  4. Quick Check:

    ArrayIndexOutOfBoundsException caught by first catch [OK]
Quick Trick: First matching catch block runs for thrown exception [OK]
Common Mistakes:
  • Thinking general catch runs first
  • Expecting exception message printed
  • Assuming no output on exception

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes