
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
Return how many elements in a 2D array are equal to 0.
1 0 3
0 2 4
→ 2
- Use nested loops
- Check every cell
- Increment counter when value is 0
public int countZeros(int[][] grid) {
int count = 0;
for (int r = 0; r < grid.length; r++) {
for (int c = 0; c < grid[r].length; c++) {
if (grid[r][c] == 0) {
count++;
}
}
}
return count;
}
- Using
grid.lengthfor columns - Off-by-one loop errors
FRQ 2 — Row Sum
Return the sum of all elements in a specified row.
Row 1 of {{2,5,1},{3,4,6}} → 13
- Traverse all columns of the given row
- Add values to a running total
public int sumRow(int[][] grid, int row) {
int sum = 0;
for (int c = 0; c < grid[row].length; c++) {
sum += grid[row][c];
}
return sum;
}
- Using
grid.lengthinstead of column length - Not initializing sum
FRQ 3 — Column Sum
sumColumn(int[][] grid, int col) that returns the sum of
all elements in the specified column.
1 2
3 4
Column 1 → 2 + 4 = 6- Loop through each row
- Add the value at the given column index
public int sumColumn(int[][] grid, int col) {
int sum = 0;
for (int r = 0; r < grid.length; r++) {
sum += grid[r][col];
}
return sum;
}
- Looping over columns instead of rows
- Using grid[col].length
FRQ 4 — Count Even Numbers
- Traverse every cell
- Check divisibility by 2
public int countEvens(int[][] grid) {
int count = 0;
for (int r = 0; r < grid.length; r++) {
for (int c = 0; c < grid[r].length; c++) {
if (grid[r][c] % 2 == 0) {
count++;
}
}
}
return count;
}
- Checking only one row
- Using integer division incorrectly
FRQ 5 — Find Maximum in the Grid
Return the largest value in a 2D array.
- Initialize max with first element
- Traverse all rows and columns
- Update max when larger value is found
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;
}
- Incorrect initialization
- Assuming all rows have same length
FRQ 6 — Row With Largest Sum
Return the index of the row with the largest sum.
- Compute sum for each row
- Track maximum sum and index
public int rowWithLargestSum(int[][] grid) {
int maxRow = 0;
int maxSum = Integer.MIN_VALUE;
for (int r = 0; r < grid.length; r++) {
int sum = 0;
for (int c = 0; c < grid[r].length; c++) {
sum += grid[r][c];
}
if (sum > maxSum) {
maxSum = sum;
maxRow = r;
}
}
return maxRow;
}
- Not resetting sum for each row
- Returning sum instead of index
FRQ 7 — Count Values Above Threshold
Return how many values in the grid are strictly greater than a given limit.
- Traverse entire grid
- Compare each value with limit
public int countAbove(int[][] grid, int limit) {
int count = 0;
for (int r = 0; r < grid.length; r++) {
for (int c = 0; c < grid[r].length; c++) {
if (grid[r][c] > limit) {
count++;
}
}
}
return count;
}
- Using >= instead of >
- Traversing only one row
FRQ 8 — Replace Negatives With Zero
Replace all negative values in the grid with 0.
- Traverse grid using nested loops
- Replace negatives with 0
public void removeNegatives(int[][] grid) {
for (int r = 0; r < grid.length; r++) {
for (int c = 0; c < grid[r].length; c++) {
if (grid[r][c] < 0) {
grid[r][c] = 0;
}
}
}
}
- Creating a new grid unnecessarily
- Missing some cells
FRQ 9 — Count Neighbors Greater Than a Value
Return how many cells have at least one neighbor (up, down, left, right) greater than a given value.
- Traverse grid
- Check valid neighbors only
- Count each cell once
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;
if (r > 0 && grid[r-1][c] > value) found = true;
if (r < grid.length - 1 && grid[r+1][c] > value) found = true;
if (c > 0 && grid[r][c-1] > value) found = true;
if (c < grid[r].length - 1 && grid[r][c+1] > value) found = true;
if (found) count++;
}
}
return count;
}
- Missing boundary checks
- Counting neighbors instead of cells
FRQ 10 — Check If Row Is Sorted
Return true if a given row is sorted in increasing order.
- Compare adjacent elements
- Return false if order breaks
public boolean isRowSorted(int[][] grid, int row) {
for (int c = 1; c < grid[row].length; c++) {
if (grid[row][c] < grid[row][c - 1]) {
return false;
}
}
return true;
}
- Comparing wrong indices
- Looping out of bounds
FRQ 11 — Count Isolated Cells
Return how many cells are greater than all four neighbors.
- Skip boundary cells
- Compare with up, down, left, right
public int countIsolated(int[][] grid) {
int count = 0;
for (int r = 1; r < grid.length - 1; r++) {
for (int c = 1; c < grid[r].length - 1; c++) {
int v = grid[r][c];
if (v > grid[r-1][c] &&
v > grid[r+1][c] &&
v > grid[r][c-1] &&
v > grid[r][c+1]) {
count++;
}
}
}
return count;
}
- Forgetting boundary checks
- Using OR instead of AND
FRQ 12 — Mirror Grid Horizontally
Reverse each row of the grid in place.
- Swap elements from both ends
- Stop at middle of each row
public void mirror(int[][] grid) {
for (int r = 0; r < grid.length; r++) {
for (int c = 0; c < grid[r].length / 2; c++) {
int temp = grid[r][c];
grid[r][c] = grid[r][grid[r].length - 1 - c];
grid[r][grid[r].length - 1 - c] = temp;
}
}
}
- Looping too far
- Swapping wrong indices
FRQ 13 — Spiral Traversal of a 2D Array
Traverse a 2D array in clockwise spiral order starting from the top-left corner and print all elements.
- Maintain four boundaries: top, bottom, left, right
- Traverse left to right, then top to bottom
- Traverse right to left, then bottom to top
- After each traversal, move the boundary inward
- Stop when boundaries cross
public void spiralTraversal(int[][] grid) {
int top = 0;
int bottom = grid.length - 1;
int left = 0;
int right = grid[0].length - 1;
while (top <= bottom && left <= right) {
for (int c = left; c <= right; c++) {
System.out.print(grid[top][c] + " ");
}
top++;
for (int r = top; r <= bottom; r++) {
System.out.print(grid[r][right] + " ");
}
right--;
if (top <= bottom) {
for (int c = right; c >= left; c--) {
System.out.print(grid[bottom][c] + " ");
}
bottom--;
}
if (left <= right) {
for (int r = bottom; r >= top; r--) {
System.out.print(grid[r][left] + " ");
}
left++;
}
}
}
- Forgetting boundary checks
- Incorrect loop conditions
- Missing elements in odd-sized matrices
- Using enhanced for-loops instead of index-based loops
FRQ 14 — Flood Fill Count
Count connected cells matching a target value using recursion.
- Check bounds and value
- Mark visited cells
- Explore all directions
public int floodCount(int[][] grid, int r, int c, int target) {
if (r < 0 || c < 0 || r >= grid.length || c >= grid[0].length)
return 0;
if (grid[r][c] != target)
return 0;
grid[r][c] = -1;
return 1 +
floodCount(grid, r+1, c, target) +
floodCount(grid, r-1, c, target) +
floodCount(grid, r, c+1, target) +
floodCount(grid, r, c-1, target);
}
- Infinite recursion
- Not marking visited cells
FRQ 15 — Create a Transposed Matrix
Return a new 2D array that is the transpose of the given matrix.
- Create array with swapped dimensions
- Assign
result[c][r] = grid[r][c]
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;
}
- Returning original array
- Incorrect dimensions
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/