2022 AP CSA FRQ 4 - Data

2022 AP CSA FRQ 4: Data

2D Array

2D Array 9 Points Total Hard
Practice Timer (22 min recommended) 22:00

Overview: This question involves the Data class, which stores and manipulates a 2D array of integers.

Topics Covered: 2D ArraysNested LoopsRandom NumbersColumn TraversalConditional Logic

Provided Code

Part A: repopulate (4 points)

4 points
Write the repopulate method. This method replaces each element of grid with a randomly generated value. The value must be a multiple of 10 that is in the range 10 to 90 inclusive (i.e., one of 10, 20, 30, 40, 50, 60, 70, 80, 90). Each value is equally likely to occur. Note: Math.random() returns a double in the range [0.0, 1.0).
Method Signature: public void repopulate()
View Solution and Rubric

Scoring Rubric

+1 Accesses all elements of grid (nested loops)
+1 Generates random integer in range [0, 8] or [1, 9]
+1 Produces values that are multiples of 10
+1 Assigns generated value to grid element

Solution

public void repopulate()
{
    for (int row = 0; row < grid.length; row++)
    {
        for (int col = 0; col < grid[0].length; col++)
        {
            grid[row][col] = (int)(Math.random() * 9 + 1) * 10;
        }
    }
}

Step-by-Step Explanation

Step 1: Use nested for loops to visit every element

Step 2: Math.random() * 9 gives [0.0, 9.0)

Step 3: Adding 1 gives [1.0, 10.0)

Step 4: Casting to int gives 1 through 9

Step 5: Multiplying by 10 gives 10 through 90 (multiples of 10)

Step 6: Assign directly to grid[row][col]

Alternative Solution

public void repopulate()
{
    for (int row = 0; row < grid.length; row++)
    {
        for (int col = 0; col < grid[0].length; col++)
        {
            grid[row][col] = ((int)(Math.random() * 9) + 1) * 10;
        }
    }
}

Part A: countIncreasingCols (5 points)

5 points
Write the countIncreasingCols method. This method returns the number of columns in grid that are in strictly increasing order from top to bottom. A column is in strictly increasing order if each element is greater than the element directly above it.
Method Signature: public int countIncreasingCols()
View Solution and Rubric

Scoring Rubric

+1 Traverses grid by column (outer loop over columns)
+1 Compares each element to element above it
+1 Correctly identifies when a column is NOT increasing
+1 Counts only columns that are strictly increasing
+1 Returns correct count

Solution

public int countIncreasingCols()
{
    int count = 0;
    
    for (int col = 0; col < grid[0].length; col++)
    {
        boolean isIncreasing = true;
        
        for (int row = 1; row < grid.length; row++)
        {
            if (grid[row][col] <= grid[row - 1][col])
            {
                isIncreasing = false;
            }
        }
        
        if (isIncreasing)
        {
            count++;
        }
    }
    
    return count;
}

Step-by-Step Explanation

Step 1: Initialize count to 0

Step 2: Outer loop iterates through columns

Step 3: For each column, assume it's increasing (isIncreasing = true)

Step 4: Inner loop starts at row 1 (need to compare with row above)

Step 5: If any element is NOT greater than the one above (<=), set flag to false

Step 6: After checking entire column, if still increasing, increment count

Step 7: Return total count

Alternative Solution

public int countIncreasingCols()
{
    int count = 0;
    
    for (int col = 0; col < grid[0].length; col++)
    {
        boolean isIncreasing = true;
        
        for (int row = 1; row < grid.length && isIncreasing; row++)
        {
            if (grid[row][col] <= grid[row - 1][col])
            {
                isIncreasing = false;
            }
        }
        
        if (isIncreasing)
        {
            count++;
        }
    }
    
    return count;
}

Common Mistakes to Avoid

No common mistakes documented for this FRQ.

Exam Tips

  • Read the problem carefully before coding.
  • Test your logic with the provided examples.
  • Watch for off-by-one errors.

Official College Board Resources

Original Question PDF Scoring Guidelines

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

tanner@apcsexamprep.com

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at tanner@apcsexamprep.com