Bird
0
0

Find the error in the following code snippet:

medium📝 Debug Q14 of 15
Java - Arrays
Find the error in the following code snippet:
int[][] grid = new int[2][3];
grid[2][1] = 5;
AArray index out of bounds error
BSyntax error in declaration
CMissing initialization of array
DNo error, code runs fine
Step-by-Step Solution
Solution:
  1. Step 1: Check array size and indices used

    Array grid has 2 rows (indices 0 and 1) and 3 columns (indices 0 to 2). Accessing grid[2][1] uses row index 2 which is out of bounds.
  2. Step 2: Identify the error type

    Accessing an invalid index causes ArrayIndexOutOfBoundsException at runtime.
  3. Final Answer:

    Array index out of bounds error -> Option A
  4. Quick Check:

    Index 2 invalid for 2-row array [OK]
Quick Trick: Check array size before accessing indices [OK]
Common Mistakes:
  • Using indices equal to array length
  • Confusing rows and columns sizes
  • Assuming arrays auto-expand

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes