AP CSA Practice Test: 2D Arrays
AP CSA Practice Test: 2D Arrays
Unit 4: Data Collections | 10 Questions | AP Computer Science A
int[][] grid = new int[3][4]; System.out.println(grid.length + " " + grid[0].length);
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.
int sum = 0;
for (int r = 0; r < grid.length; r++) {
for (int c = 0; c < grid[0].length; c++) {
sum += grid[r][c];
}
}
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.
for(r=0;r
for(c=0;c
for(r=0;r0;c--)
for(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).
int[][] m = {{1,2,3},{4,5,6},{7,8,9}};
System.out.println(m[2][0] + m[0][2]);
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.
int[][] grid = {{1,2},{3,4},{5,6}};
int total = 0;
for (int[] row : grid) {
total += row[0];
}
System.out.println(total);
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.
// grid is int[][], target is int
for(int[] r:grid) for(int v:r) if(v==target) count++;
for(int r:grid) if(r==target) count++;
for(int c=0;c
for(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.
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]);
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.
int sum = 0;
for (int i = 0; i < grid.length; i++) {
sum += grid[i][i];
}
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.
int[][] grid = {{1,2,3},{4,5,6}};
int count = 0;
for (int[] row : grid) {
count += row.length;
}
System.out.println(count);
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.
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);
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.
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.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
tanner@apcsexamprep.com
Courses
AP CSA, CSP, & Cybersecurity
Response Time
Within 24 hours
Prefer email? Reach me directly at tanner@apcsexamprep.com