Bird
0
0

Identify the error in the following code snippet:

medium📝 Debug Q6 of 15
Java - Arrays
Identify the error in the following code snippet:
int[] arr = new int[5];
arr[5] = 10;
ASyntax error: cannot assign value to array
BCompilation error: invalid array declaration
CArrayIndexOutOfBoundsException at runtime
DNo error, code runs fine
Step-by-Step Solution
Solution:
  1. Step 1: Understand array indexing limits

    Array of size 5 has valid indices 0 to 4.
  2. Step 2: Identify invalid index usage

    Accessing arr[5] is outside valid range, causing runtime exception.
  3. Final Answer:

    ArrayIndexOutOfBoundsException at runtime -> Option C
  4. Quick Check:

    Invalid index access causes runtime exception [OK]
Quick Trick: Array indices go from 0 to length-1 [OK]
Common Mistakes:
  • Assuming index 5 is valid
  • Expecting compile-time error
  • Ignoring runtime exceptions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes