Are you taking CLEP classes? Try clep.ai
| 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

Mark
< Prev
Next >

What is the cause of the runtime exception in the following code segment?

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 print statement should use println() instead of print().

B

The array elements are of type int, which cannot be printed directly.

C

The array ‘matrix’ is declared incorrectly.

D

The loop conditions use ‘<=’ instead of ‘<’, causing an ArrayIndexOutOfBoundsException.

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

Question Leaderboard

Rank
User
Correct Count
Attempt Count
Total Time
Score
#1deltasmpserver11 0m 00s 100
#2y.seong202711 0m 24s 76
#3theofanusmischa11 1m 22s 18
Items per page:
10
1 – 3 of 3

AI Tutor

How can I help?

APFIVE © 2020.
Email: apfive@apfive.org|Privacy Policy|