Choosing how to earn college credit? CLEP vs AP guide
| preferred AP College board partner for AP classes
AP Computer Science A/Unit 4: Data Collections
Start Practice TestPractice Test
About Exam
easy

Summing a 2D Array Diagonal

Mark
< Prev
Next >

Consider the following code segment.

int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int result = 0;
for (int row = 0; row < matrix.length; row++) {
    for (int col = 0; col < matrix[row].length; col++) {
        if (row == col) {
            result += matrix[row][col];
        }
    }
}
System.out.println(result);

What is printed as a result of executing the code segment?

A

9

B

45

C

15

D

6

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

Question Leaderboard

Not enough data yet to show leaderboard.

No comments yet. Be the first to comment!

AI Tutor

How can I help?

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