| preferred AP College board partner for AP classes
AP Computer Science A/Unit 8: 2D Array
Start Practice TestPractice Test
About Exam
easy Solved by 3 students
2D Array Loop Boundary Error
< Prev
Next >

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.

Hint
Did You Know?
Explain Why
Explain All Answers
Check Answer
Show Correct Answer
Report Question

Question Leaderboard

Rank
User
Correct Count
Attempt Count
Time
Score
#1deltasmpserver11 0m 00s 100
#2y.seong202711 0m 24s 76
#3theofanusmischa11 1m 22s 18
Items per page:
10
1 – 3 of 3
No comments yet. Be the first to comment!

AI Tutor

How can I help?

APFIVE © 2020.
Email: [email protected]|Privacy Policy