2011 AP CSA FRQ 4: RouteCipher - Solution

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.

Try First! Attempt before viewing solution.

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

✓ Row-major fill, column-major read for encryption
✓ Pad with 'A' if message is shorter than block
✓ Use substring(i, i+1) for single character
✓ Column-major: outer loop columns, inner loop rows

Common Mistakes

Wrong traversal order (row vs column major)
Not padding shorter messages
Off-by-one in substring indices
Not calling fillBlock before encrypting
Practice Similar: 2019 FRQ 4 | 2022 FRQ 4 | 2023 FRQ 4

Official Resources

Author: Tanner Crow - AP CS Teacher, 11+ years

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]