
2D arrays appear frequently in AP Computer Science A FRQs because they test a student’s ability to traverse grids, navigate rows and columns, process neighbors, and design algorithms using nested loops.
This page provides, exam-style FRQs focused entirely on 2D array logic.
Why Practice 2D Array FRQs?
AP CSA often includes free-response questions involving:
- Row-by-row traversal
- Column-by-column traversal
- Counting values in a grid
- Searching for patterns
- Neighbor-based calculations
- Modifying 2D arrays
- Nested loop logic
Mastering these skills significantly improves FRQ performance.
How to Use This FRQ Page
- Try each problem independently first
- Compare your approach with the provided solution
- Study the Common Mistakes section
- Reattempt until you can solve quickly and consistently
- Use these as real exam warm-ups
FRQ 1 — Count Zeros in a 2D Array (Easy)
Problem:
Write a method countZeros(int[][] grid) that returns how many elements in a 2D array are equal to 0.
Example:
Input:
1 0 3
0 2 4
Output → 2
Approach:
- Use nested loops
- Count elements equal to zero
Java Solution:
public int countZeros(int[][] grid) {
int count = 0;
for (int r = 0; r < grid.length; r++) {
for (int c = 0; c < grid[0].length; c++) {
if (grid[r][c] == 0) {
count++;
}
}
}
return count;
}
Common Mistakes:
- Using
grid.lengthfor both rows and columns - Off-by-one errors
- Returning premature values
FRQ 2 — Row Sum (Easy)
Problem:
Write a method sumRow(int[][] grid, int row) that returns the sum of all elements in the given row.
Assume row is valid.
Example:
Row 1 of
2 5 1
3 4 6
→ 3 + 4 + 6 = 13
Approach:
- Loop through all columns in the specified row
- Accumulate the sum
Java Solution:
public int sumRow(int[][] grid, int row) {
int sum = 0;
for (int col = 0; col < grid[row].length; col++) {
sum += grid[row][col];
}
return sum;
}
Common Mistakes:
- Using grid.length instead of grid[row].length
- Forgetting to initialize sum
- Handling invalid rows (not required here)
FRQ 3 — Find Maximum in the Grid (Medium)
Problem:
Write findMax(int[][] grid) that returns the largest value in the entire 2D array.
Approach:
- Set max to first element
- Traverse all rows and columns
- Update when a larger value is found
Java Solution:
public int findMax(int[][] grid) {
int max = grid[0][0];
for (int r = 0; r < grid.length; r++) {
for (int c = 0; c < grid[r].length; c++) {
if (grid[r][c] > max) {
max = grid[r][c];
}
}
}
return max;
}
Common Mistakes:
- Using grid[0].length for every row (irregular arrays fail)
- Forgetting to compare all cells
- Incorrect initialization of max
FRQ 4 — Count Neighbors Greater Than a Value (Hard)
Problem:
Write a method countGreaterNeighbors(int[][] grid, int value) that returns how many cells have at least one neighbor (up/down/left/right) greater than value.
Boundary edges must be handled correctly.
Example:
If a cell has neighbor > value → count it.
Approach:
- Traverse grid
- For each cell, check valid neighbors
- Count if any neighbor is > value
Java Solution:
public int countGreaterNeighbors(int[][] grid, int value) {
int count = 0;
for (int r = 0; r < grid.length; r++) {
for (int c = 0; c < grid[r].length; c++) {
boolean found = false;
// up
if (r > 0 && grid[r-1][c] > value) found = true;
// down
if (r < grid.length - 1 && grid[r+1][c] > value) found = true;
// left
if (c > 0 && grid[r][c-1] > value) found = true;
// right
if (c < grid[r].length - 1 && grid[r][c+1] > value) found = true;
if (found) count++;
}
}
return count;
}
Common Mistakes:
- Forgetting boundary checks
- Using OR instead of AND incorrectly
- Counting neighbors instead of counting cells with neighbors
FRQ 5 — Create a Transposed Matrix (Hard)
Problem:
Write a method transpose(int[][] grid) that returns a new 2D array representing the transpose of the original matrix.
Transpose rule:result[c][r] = grid[r][c]
Approach:
- Create array of size
[cols][rows] - Swap row/column indices when copying
Java Solution:
public int[][] transpose(int[][] grid) {
int rows = grid.length;
int cols = grid[0].length;
int[][] result = new int[cols][rows];
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
result[c][r] = grid[r][c];
}
}
return result;
}
Common Mistakes:
- Using same row/column count
- Returning the original array
- Incorrect index assignment
More 2D Array FRQs Coming Soon
- Count increasing neighbors
- Find the row with largest sum
- Spiral traversal
- Flood-fill style problems
- Count isolated values
Related FRQ Pages
- Arrays – FRQ Practice
- ArrayList – FRQ Practice
- Strings – FRQ Practice
- Mixed FRQs
- Mock FRQ Sets
(Add links once pages are published)
Need Help With AP CSA?
Get personalized 1-on-1 AP CSA tutoring from a Java expert with 15+ years of experience, including complete FRQ training.
WhatsApp: +91 9853166385
Book a free demo: javatutoronline.com/contact-us/