Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q4 of 15
Java - Exception Handling
What will be the output of this code?
try {
  int[] arr = {1, 2, 3};
  System.out.println(arr[3]);
} catch (ArrayIndexOutOfBoundsException e) {
  System.out.println("Index error");
} finally {
  System.out.println("Done");
}
AArrayIndexOutOfBoundsException\nDone
B3\nDone
CIndex error\nDone
DDone
Step-by-Step Solution
Solution:
  1. Step 1: Identify exception thrown

    Accessing arr[3] causes ArrayIndexOutOfBoundsException because valid indexes are 0-2.
  2. Step 2: Catch block and finally execution

    The exception is caught, printing "Index error". Then finally prints "Done".
  3. Final Answer:

    Index error\nDone -> Option C
  4. Quick Check:

    Exception caught + finally runs = Index error\nDone [OK]
Quick Trick: Catch runs on exception; finally always runs [OK]
Common Mistakes:
  • Assuming arr[3] prints 3
  • Thinking exception message prints automatically
  • Ignoring finally block output

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes