Ap Csa 2022 Frq 4 Data

FRQ Archive2022 FRQs › FRQ 4: Data
2022 AP CSA • 2D Array

AP CSA 2022 FRQ 4: Data

Complete solution, scoring rubric, and walkthrough — verified from the official College Board PDF

9 Points Medium-Hard Unit 4
Question Type 2D Array
Skills Tested 2D array nested loops, Math.random() with constraints, rejection sampling, column traversal
Difficulty Medium-Hard
Recommended Time 22 minutes

What This Problem Asks

2022 AP CSA FRQ 4 Data asks students to write two methods for a 2D array class. repopulate fills every cell with a random integer between 1 and MAX that is divisible by 10 but not by 100 (using rejection sampling or direct formula). countIncreasingCols counts how many columns have non-decreasing values from top to bottom.

What This FRQ Tests

This FRQ tests Unit 4: 2D Arrays and Unit 2: Selection & Iteration (Math.random(), modulo operator, nested loops, boolean flags).

Official Question & PDF

Download Full FRQ PDF →

PDF cannot display on this device. Open PDF directly →

Timer 22:00

Provided Code

public class Data
{
    public static final int MAX = /* value not shown */;
    private int[][] grid;

    /** Fills all elements of grid with randomly generated values.
     *  Each value: between 1 and MAX inclusive, divisible by 10, NOT divisible by 100.
     *  Precondition: grid is not null, grid has at least one element. */
    public void repopulate() { /* to be implemented in part (a) */ }

    /** Returns the number of columns in grid that are in increasing order.
     *  A column is in increasing order if each row's element >= previous row's element.
     *  Precondition: grid is not null, grid has at least one element. */
    public int countIncreasingCols() { /* to be implemented in part (b) */ }
}

Part A — 4 Points

Write repopulate, which fills every element of grid with a randomly generated value between 1 and MAX inclusive, divisible by 10, and not divisible by 100. All valid values must have equal probability of being chosen.

Write Your Solution

Drag corner to expand ▽

 

Scoring Rubric (Part A — 4 points)

+1 Traverses all elements of grid with no bounds errors
+1 Generates a random value using Math.random() scaled to 1–MAX, cast to int
+1 Correctly identifies values that are divisible by 10 AND NOT divisible by 100
+1 Assigns only valid values to grid elements (algorithm — loop until valid or rejection sampling)

Solution (Rejection Sampling)

public void repopulate()
{
    for (int r = 0; r < grid.length; r++)
    {
        for (int c = 0; c < grid[0].length; c++)
        {
            int val;
            do
            {
                val = (int)(Math.random() * MAX) + 1;
            } while (val % 10 != 0 || val % 100 == 0);
            grid[r][c] = val;
        }
    }
}
Alternative (direct formula): Generate a multiple of 10 that isn’t a multiple of 100 directly. Count valid multiples (10,20,...,90,110,...) = MAX/10 - MAX/100. Pick one at random. Rejection sampling is simpler to write under time pressure.

Part B — 5 Points

Write countIncreasingCols, which returns the number of columns in grid where each element in each row after the first is >= the element in the previous row.

Write Your Solution

Drag corner to expand ▽

 

Scoring Rubric (Part B — 5 points)

+1 Traverses each column with no bounds errors
+1 Compares adjacent elements within a column using < (if next element is LESS, not increasing)
+1 Uses a flag or early return to mark a column as not-increasing
+1 Correctly counts only columns that are in increasing order (algorithm)
+1 Returns the count

Solution

public int countIncreasingCols()
{
    int count = 0;
    for (int c = 0; c < grid[0].length; c++)
    {
        boolean increasing = true;
        for (int r = 1; r < grid.length; r++)
        {
            if (grid[r][c] < grid[r-1][c])
            {
                increasing = false;
            }
        }
        if (increasing)
        {
            count++;
        }
    }
    return count;
}
Note: A column is increasing if each element >= the element before it. So the check for NOT-increasing is grid[r][c] < grid[r-1][c]. “Increasing order” here allows equal consecutive values.

Common Mistakes to Avoid

Using > instead of < for the not-increasing check

A column is in increasing order if each next element is >= the previous. If grid[r][c] < grid[r-1][c], the column is not increasing. Checking > instead gets the logic backwards.

Looping rows from 0 (instead of 1) causing index out of bounds

When comparing grid[r][c] to grid[r-1][c], the inner loop must start at row 1, not row 0.

Wrong

for (int r = 0; r < grid.length; r++) {
    if (grid[r][c] < grid[r-1][c])  // ArrayIndexOutOfBounds when r=0!

Correct

for (int r = 1; r < grid.length; r++) {  // start at 1
    if (grid[r][c] < grid[r-1][c])

Exam Tips

For repopulate, rejection sampling (do-while with a condition check) is simpler to write than direct formula generation. Both earn full credit.
For countIncreasingCols, the outer loop should be columns and the inner loop should be rows — you are checking each column for the increasing property.
“Increasing order” includes equal consecutive elements (>=). Only strictly-less consecutive values break the property.

Scoring Summary

Part Method Points
Part A Write 4
Part B Write 5
Total 9

Want the Complete 2022 FRQ Solutions?

Browse all past AP CSA FRQs with full solutions and scoring breakdowns.

FRQ Archive →

Related FRQs

2D Array 2021 FRQ 4: ArrayResizer — Filter 2D array rows with no zero values 2D Array 2023 FRQ 4: BoxOfCandy — 2D array column operations and specified traversal

Study the Concepts

Unit 4 Study Guide →   Unit 2 Study Guide →

Struggling with FRQs? Get 1-on-1 Help

Work directly with Tanner — AP CS teacher with 11+ years experience and 1,845+ verified tutoring hours. 54.5% of students score 5s (vs. 25.5% national average).

5.0Rating (451+ reviews)
1,845+Verified Hours
54.5%Score 5s

Book a Session ($150/hr) →

5-session packages at $125/hr. Venmo, Zelle, PayPal, or credit card.

Frequently Asked Questions

What does 2022 AP CSA FRQ 4 Data test?

FRQ 4 tests 2D array traversal and Math.random(). repopulate fills a 2D array with random values meeting specific divisibility criteria. countIncreasingCols checks each column for non-decreasing order.

How many points is FRQ 4 Data worth?

9 points: 4 for Part A (repopulate) and 5 for Part B (countIncreasingCols).

Why is rejection sampling valid for repopulate?

Generating a random value in range 1-MAX and rejecting it if invalid (not divisible by 10, or divisible by 100) is a valid approach that the College Board accepts. It’s simpler to write under exam conditions than direct formula generation.

What does 'increasing order' mean in countIncreasingCols?

Each element must be greater than or equal to the element in the row above it. Equal consecutive values are allowed. A column with only one row is always in increasing order.

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

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]