2011 AP CSA FRQ 4: RouteCipher
Topic: 2D Arrays & Encryption
Skills: 2D array construction, column-major traversal, string building
Unit: Unit 4 (Data Collections) (2025-2026 AP CSA)
Points: 9 | Time: ~22 minutes
Part (a)
Write the fillBlock method that fills a 2D array with characters from a string.
Solution
private void fillBlock(String str)
{
int index = 0;
for (int r = 0; r < numRows; r++)
{
for (int c = 0; c < numCols; c++)
{
if (index < str.length())
{
letterBlock[r][c] = str.substring(index, index + 1);
}
else
{
letterBlock[r][c] = "A";
}
index++;
}
}
}Part (b)
Write the encryptMessage method that encrypts using column-based reading.
Solution
public String encryptMessage(String message)
{
String result = "";
fillBlock(message);
for (int c = 0; c < numCols; c++)
{
for (int r = 0; r < numRows; r++)
{
result += letterBlock[r][c];
}
}
return result;
}Key Concepts
Common Mistakes
Official Resources
Author: Tanner Crow - AP CS Teacher, 11+ years