Bird
0
0

Identify the problem in this Java code:

medium📝 Debug Q7 of 15
Java - Exception Handling
Identify the problem in this Java code:
try {
int[] nums = new int[3];
nums[3] = 10;
} catch (NullPointerException e) {
System.out.println("Null pointer");
}
ACatch block does not handle ArrayIndexOutOfBoundsException
BArray size is incorrect
CTry block syntax is wrong
DNo exception will occur
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the exception thrown

    Accessing nums[3] causes ArrayIndexOutOfBoundsException, not NullPointerException.
  2. Step 2: Check catch block exception type

    Catch block only handles NullPointerException, so the actual exception is not caught.
  3. Final Answer:

    Catch block does not handle ArrayIndexOutOfBoundsException -> Option A
  4. Quick Check:

    Catch type mismatch = uncaught exception [OK]
Quick Trick: Catch block must match thrown exception type [OK]
Common Mistakes:
  • Assuming NullPointerException covers all exceptions
  • Thinking array size is wrong
  • Believing no exception occurs

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes