2022 AP CSA FRQ 4 - Data
2022 AP CSA FRQ 4: Data
2D Array
Overview: This question involves the Data class, which stores and manipulates a 2D array of integers.
Provided Code
Part A: repopulate (4 points)
4 pointspublic void repopulate()
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 pointspublic int countIncreasingCols()
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.
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.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
tanner@apcsexamprep.com
Courses
AP CSA, CSP, & Cybersecurity
Response Time
Within 24 hours
Prefer email? Reach me directly at tanner@apcsexamprep.com