• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar

JavaTutorOnline

1-on-1 Online Java Training by a Senior Software Engineer

  • Home
  • AP CSA
    • FRQ Practice
      • Arrays
      • ArrayList
      • Strings
      • 2D Arrays
  • Courses
  • Tutorials
    • Java
    • Servlets
    • Struts
    • Spring
    • Webservice
  • FAQ
  • Testimonials
  • Blog
  • CONTACT US

2D Arrays – FRQ Practice (AP Computer Science A)

2D Arrays FRQ Practice Ap Computer Science

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:

Return how many elements in a 2D array are equal to 0.

Example:

1 0 3
0 2 4
→ 2
    
Approach:
  • Use nested loops
  • Check every cell
  • Increment counter when value is 0
Java Solution:

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;
}
    
Common Mistakes:
  • Using grid.length for columns
  • Off-by-one loop errors

FRQ 2 — Row Sum

Easy
Problem:

Return the sum of all elements in a specified row.

Example:

Row 1 of {{2,5,1},{3,4,6}} → 13

Approach:
  • Traverse all columns of the given row
  • Add values to a running total
Java Solution:

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;
}
    
Common Mistakes:
  • Using grid.length instead of column length
  • Not initializing sum

FRQ 3 — Column Sum

Easy
Problem:
Write sumColumn(int[][] grid, int col) that returns the sum of all elements in the specified column.
Example:
1 2
3 4
    
Column 1 → 2 + 4 = 6
Approach:
  • Loop through each row
  • Add the value at the given column index
Java Solution:

public int sumColumn(int[][] grid, int col) {
    int sum = 0;
    for (int r = 0; r < grid.length; r++) {
        sum += grid[r][col];
    }
    return sum;
}
    
Common Mistakes:
  • Looping over columns instead of rows
  • Using grid[col].length

FRQ 4 — Count Even Numbers

Easy
Problem:
Return how many even numbers exist in a 2D array.
Approach:
  • Traverse every cell
  • Check divisibility by 2
Java Solution:

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;
}
    
Common Mistakes:
  • Checking only one row
  • Using integer division incorrectly

FRQ 5 — Find Maximum in the Grid

Medium
Problem:

Return the largest value in a 2D array.

Approach:
  • Initialize max with first element
  • Traverse all rows and columns
  • Update max when 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:
  • Incorrect initialization
  • Assuming all rows have same length

FRQ 6 — Row With Largest Sum

Medium
Problem:

Return the index of the row with the largest sum.

Approach:
  • Compute sum for each row
  • Track maximum sum and index
Java Solution:

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;
}
    
Common Mistakes:
  • Not resetting sum for each row
  • Returning sum instead of index

FRQ 7 — Count Values Above Threshold

Medium
Problem:

Return how many values in the grid are strictly greater than a given limit.

Approach:
  • Traverse entire grid
  • Compare each value with limit
Java Solution:

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;
}
    
Common Mistakes:
  • Using >= instead of >
  • Traversing only one row

FRQ 8 — Replace Negatives With Zero

Medium
Problem:

Replace all negative values in the grid with 0.

Approach:
  • Traverse grid using nested loops
  • Replace negatives with 0
Java Solution:

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;
            }
        }
    }
}
    
Common Mistakes:
  • Creating a new grid unnecessarily
  • Missing some cells

FRQ 9 — Count Neighbors Greater Than a Value

Hard
Problem:

Return how many cells have at least one neighbor (up, down, left, right) greater than a given value.

Approach:
  • Traverse grid
  • Check valid neighbors only
  • Count each cell once
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;

            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;
}
    
Common Mistakes:
  • Missing boundary checks
  • Counting neighbors instead of cells

FRQ 10 — Check If Row Is Sorted

Hard
Problem:

Return true if a given row is sorted in increasing order.

Approach:
  • Compare adjacent elements
  • Return false if order breaks
Java Solution:

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;
}
    
Common Mistakes:
  • Comparing wrong indices
  • Looping out of bounds

FRQ 11 — Count Isolated Cells

Hard
Problem:

Return how many cells are greater than all four neighbors.

Approach:
  • Skip boundary cells
  • Compare with up, down, left, right
Java Solution:

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;
}
    
Common Mistakes:
  • Forgetting boundary checks
  • Using OR instead of AND

FRQ 12 — Mirror Grid Horizontally

Hard
Problem:

Reverse each row of the grid in place.

Approach:
  • Swap elements from both ends
  • Stop at middle of each row
Java Solution:

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;
        }
    }
}
    
Common Mistakes:
  • Looping too far
  • Swapping wrong indices

FRQ 13 — Spiral Traversal of a 2D Array

Hard
Problem:

Traverse a 2D array in clockwise spiral order starting from the top-left corner and print all elements.

Approach:
  • 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
Java Solution:

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++;
        }
    }
}
    
Common Mistakes:
  • 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

Hard
Problem:

Count connected cells matching a target value using recursion.

Approach:
  • Check bounds and value
  • Mark visited cells
  • Explore all directions
Java Solution:

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);
}
    
Common Mistakes:
  • Infinite recursion
  • Not marking visited cells

FRQ 15 — Create a Transposed Matrix

Hard
Problem:

Return a new 2D array that is the transpose of the given matrix.

Approach:
  • Create array with swapped dimensions
  • Assign result[c][r] = grid[r][c]
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:
  • 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/

Primary Sidebar

Mr Chinmay

Chinmay Patel
Online Java Tutor-Demo Class

Phone & Whatsapp +919853166385
javatution@gmail.com

Recent Posts

  • How to Learn Java in One day? | Crash Course | JavaTutorOnline
  • Constructor in Java and Overloaded Constructor Example Program
  • Important Interview Questions on Java Multithreading
  • React Spring Boot Web Services Integration
  • Spring Boot RESTful Web Services Example
  • Top Spring MVC Interview Questions and Answers for Developers
  • Top Spring Core Interview Questions and Answers for Developers
  • Host Java Web Apps for Free on Mobile with Tomcat and Termux
  • How to Deploy Java Web Application on Aws EC2 with Elastic IP
  • Simple Jsp Servlet Jdbc User Registration using Tomcat Mysql and Eclipse
Copyright © 2026 JavaTutorOnline