2012 AP CSA FRQ 3: GrayImage
Topic: 2D Arrays & Image Processing
Skills: 2D array traversal, counting, pixel manipulation
Unit: Unit 4 (Data Collections) (2025-2026 AP CSA)
Points: 9 | Time: ~22 minutes
Part (a)
Write the countWhitePixels method.
Solution
public int countWhitePixels()
{
int count = 0;
for (int r = 0; r < pixelValues.length; r++)
{
for (int c = 0; c < pixelValues[0].length; c++)
{
if (pixelValues[r][c] == 255)
{
count++;
}
}
}
return count;
}Part (b)
Write the processImage method that reduces pixel values.
Solution
public void processImage()
{
for (int r = 0; r < pixelValues.length; r++)
{
for (int c = 0; c < pixelValues[0].length; c++)
{
int newValue = pixelValues[r][c] - pixelValues[r][c] / 2;
pixelValues[r][c] = newValue;
}
}
}Key Concepts
Common Mistakes
Official Resources
Author: Tanner Crow - AP CS Teacher, 11+ years