Unit 4 Day 22: 2D Array Patterns

Unit 4, Section 4.13
Day 22 Practice
Focus: Common 2D array patterns

Practice Question

Consider the following code segment:

What is sum?
int[][] grid = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int sum = 0;
for (int i = 0; i < grid.length; i++) {
    sum += grid[i][i];
}
Why This Answer?

grid[0][0]=1, grid[1][1]=5, grid[2][2]=9. Diagonal sum = 1+5+9 = 15.

Why Not the Others?

A) First row sum.

B) Middle row sum.

D) Last row sum.

E) All elements sum.

AP Exam Tip

grid[i][i] accesses the main diagonal of a square array.

Keep Practicing!

Consistent daily practice is the key to AP CSA success.

Try Study Games Practice FRQs
Back to blog

Leave a comment

Please note, comments need to be approved before they are published.