AP CSA Practice Test: 2D Arrays

AP CSA Practice Test: 2D Arrays

Unit 4: Data Collections | 10 Questions | AP Computer Science A

Question 1
What is the output?
int[][] grid = new int[3][4];
System.out.println(grid.length + " " + grid[0].length);
A3 4
B4 3
C12 12
D4 4

Explanation: grid.length = number of rows (3). grid[0].length = number of columns in row 0 (4). A 2D array declared as [rows][cols].

Common Mistake: Confusing rows and columns. grid.length is always the number of rows.

Question 2
What does this code do?
int sum = 0;
for (int r = 0; r < grid.length; r++) {
    for (int c = 0; c < grid[0].length; c++) {
        sum += grid[r][c];
    }
}
ASums the first row
BSums the first column
CSums all elements
DSums the diagonal

Explanation: The outer loop iterates over all rows, the inner loop over all columns. Together they visit every element in the 2D array, summing them all.

Common Mistake: Confusing row-major traversal (all elements) with single-row or single-column traversal.

Question 3
Which code traverses a 2D array in column-major order?
Afor(r=0;r
Bfor(c=0;c
Cfor(r=0;r0;c--)
Dfor(r=rows;r>0;r--) for(c=0;c

Explanation: Column-major means the outer loop iterates over columns and the inner loop iterates over rows. This processes all elements in column 0 first, then column 1, etc.

Common Mistake: Confusing row-major (outer=row) with column-major (outer=column).

Question 4
What is the output?
int[][] m = {{1,2,3},{4,5,6},{7,8,9}};
System.out.println(m[2][0] + m[0][2]);
A10
B8
C6
D12

Explanation: m[2][0] = row 2, col 0 = 7. m[0][2] = row 0, col 2 = 3. 7 + 3 = 10.

Common Mistake: Mixing up [row][col] ordering. First index is always the row.

Question 5
What does this code print?
int[][] grid = {{1,2},{3,4},{5,6}};
int total = 0;
for (int[] row : grid) {
    total += row[0];
}
System.out.println(total);
A21
B9
C6
D3

Explanation: Enhanced for loop gives each row as an int[]. row[0] is the first element of each row: 1 + 3 + 5 = 9. This sums the first column.

Common Mistake: Thinking row[0] gives the first row. In enhanced for, 'row' IS a row, and row[0] is its first element.

Question 6
Which code correctly counts how many elements in a 2D array equal a target value?
// grid is int[][], target is int
Afor(int[] r:grid) for(int v:r) if(v==target) count++;
Bfor(int r:grid) if(r==target) count++;
Cfor(int c=0;c
Dfor(int r:grid) for(int c:r) if(grid[r][c]==target) count++;

Explanation: A: Correct. Outer enhanced for gives each row as int[], inner gives each value. B: compile error (r would be int[], not int). C: grid[c] is a row, not an int. D: r is already an int[], can't use as index.

Common Mistake: Not understanding that enhanced for on a 2D array gives rows (int[]) first.

Question 7
What is the output?
int[][] arr = new int[2][3];
for (int i = 0; i < 2; i++)
    for (int j = 0; j < 3; j++)
        arr[i][j] = i + j;
System.out.println(arr[1][2]);
A3
B2
C5
D1

Explanation: arr[1][2] = 1 + 2 = 3. The element at row 1, column 2 stores the sum of its indices.

Common Mistake: Confusing the formula. arr[i][j] = i + j means arr[1][2] = 3.

Question 8
What does this code compute for a square matrix?
int sum = 0;
for (int i = 0; i < grid.length; i++) {
    sum += grid[i][i];
}
ASum of first row
BSum of first column
CSum of main diagonal
DSum of all elements

Explanation: When the row index equals the column index (grid[i][i]), you are accessing the main diagonal: (0,0), (1,1), (2,2), etc.

Common Mistake: Not recognizing the [i][i] pattern as the diagonal.

Question 9
What is the output?
int[][] grid = {{1,2,3},{4,5,6}};
int count = 0;
for (int[] row : grid) {
    count += row.length;
}
System.out.println(count);
A6
B2
C3
D5

Explanation: Two rows, each with length 3. 3 + 3 = 6. This counts the total number of elements in the 2D array.

Common Mistake: Adding grid.length (rows) instead of each row's length.

Question 10
Consider the following. What is printed?
int[][] m = {{9,8,7},{6,5,4},{3,2,1}};
int val = m[0][0];
for (int r = 0; r < 3; r++) {
    for (int c = 0; c < 3; c++) {
        if (m[r][c] < val)
            val = m[r][c];
    }
}
System.out.println(val);
A9
B1
C5
D3

Explanation: This is a standard find-minimum algorithm for a 2D array. Starting at 9, it compares every element and keeps the smallest. The minimum value is 1.

Common Mistake: Stopping early. The algorithm checks ALL elements, not just a row or diagonal.

0% 0/10

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

tanner@apcsexamprep.com

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at tanner@apcsexamprep.com