Bird
0
0

Find the error in this code snippet:

medium📝 Debug Q14 of 15
Java - Exception Handling
Find the error in this code snippet:
try {
  int[] arr = new int[3];
  arr[5] = 10;
} catch (ArrayIndexOutOfBoundsException e) {
  System.out.println("Index error");
}
AArray size is too small
BNo error, code runs fine
CCatch block syntax is wrong
DException type is incorrect
Step-by-Step Solution
Solution:
  1. Step 1: Analyze array usage

    Array declared with size 3, valid indices are 0,1,2; index 5 is out of bounds.
  2. Step 2: Understand exception thrown

    Accessing index 5 causes ArrayIndexOutOfBoundsException, which is correctly caught.
  3. Final Answer:

    Array size is too small -> Option A
  4. Quick Check:

    Index 5 invalid for size 3 = C [OK]
Quick Trick: Check array size vs accessed index to find error [OK]
Common Mistakes:
  • Thinking catch syntax is wrong
  • Assuming no error occurs
  • Confusing exception types

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes