2D Array Loop Boundary Error
Identify the error that causes a runtime exception in the code above.
public class Debug2D {
public static void main(String[] args) {
int[][] matrix = { {1, 2, 3}, {4, 5, 6} };
for (int i = 0; i <= matrix.length; i++) {
for (int j = 0; j <= matrix[i].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
}
A
The array elements are of type int, which cannot be printed directly.
B
The loop conditions use ‘<=’ instead of ‘<’, causing an ArrayIndexOutOfBoundsException.
C
The print statement should use println() instead of print().
D
The array ‘matrix’ is declared incorrectly.
Question Leaderboard
| Rank | |||||
|---|---|---|---|---|---|
| #1 | deltasmpserver | 1 | 1 | 0m 00s | 100 |
| #2 | y.seong2027 | 1 | 1 | 0m 24s | 76 |
| #3 | theofanusmischa | 1 | 1 | 1m 22s | 18 |
Items per page:
10
1 – 3 of 3
APFIVE