AP CSA 2016 FRQ 4 Stringformatter

FRQ Archive2016 FRQs › FRQ 4: StringFormatter
2016 AP CSA • String Manipulation

AP CSA 2016 FRQ 4: StringFormatter

Complete solution, scoring rubric, and walkthrough — verified from the official College Board PDF

9 Points Hard String Manipulation Classic Format (Pre-2019)
Question Type String Manipulation
Key Skills Write totalLetters() (sum of all word lengths in a list), leftoverSpaces() (total spaces needed to fill a line), and for
Difficulty Hard
AP CSA Units Unit 2: Selection & Iteration (loop with arithmetic), Unit 4: Data Collections (ArrayList traversal), String building.

What This Problem Asks

Write totalLetters() (sum of all word lengths in a list), leftoverSpaces() (total spaces needed to fill a line), and format() (distribute spaces evenly between words, with leftmost gaps receiving any extra space).

Official Question & PDF

Download Full 2016 FRQ PDF →

PDF cannot display here. Open PDF directly →

Provided Code

public class StringFormatter
{
    public static int totalLetters(ArrayList wordList)
    { /* to be implemented in part (a) */ }

    public static int leftoverSpaces(ArrayList wordList, int formattedLen)
    { /* to be implemented in part (b) */ }

    public static String format(ArrayList wordList, int formattedLen)
    { /* to be implemented in part (c) */ }
}
Timer 22:00

Part A — totalLetters (2 Points)

Write totalLetters(). Return the total number of characters across all words in wordList.

Drag corner to expand ▽

 

Scoring Rubric

+1 Iterates over wordList
+1 Accumulates word lengths and returns the sum

Solution

public static int totalLetters(ArrayList wordList)
{
    int total = 0;
    for (String word : wordList)
    {
        total += word.length();
    }
    return total;
}
Simple accumulation loop. Call totalLetters() from the other methods — don't recompute it inline.

Part B — leftoverSpaces (2 Points)

Write leftoverSpaces(). Return the number of spaces that must be distributed across the gaps between words to reach formattedLen total length.

Drag corner to expand ▽

 

Scoring Rubric

+1 Calls totalLetters() to get character count
+1 Returns formattedLen minus totalLetters() result

Solution

public static int leftoverSpaces(ArrayList wordList, int formattedLen)
{
    return formattedLen - totalLetters(wordList);
}
Total spaces = target length minus total letter count. Must call totalLetters() for the rubric dependency point.

Part C — format (5 Points)

Write format(). Distribute leftoverSpaces() evenly across the gaps between words (wordList.size()-1 gaps). The leftmost gaps receive one extra space each when the total does not divide evenly.

Drag corner to expand ▽

 

Scoring Rubric

+1 Calls leftoverSpaces() to get total spaces
+1 Calculates base spaces per gap (total / numGaps)
+1 Calculates extra spaces (total % numGaps)
+1 Assigns extra space to leftmost gaps
+1 Builds and returns the correctly formatted string (algorithm)

Solution

public static String format(ArrayList wordList, int formattedLen)
{
    int numGaps = wordList.size() - 1;
    int totalSpaces = leftoverSpaces(wordList, formattedLen);
    int baseSpaces = totalSpaces / numGaps;
    int extraSpaces = totalSpaces % numGaps;

    String result = wordList.get(0);

    for (int i = 1; i < wordList.size(); i++)
    {
        int spaces = baseSpaces + (i - 1 < extraSpaces ? 1 : 0);
        for (int s = 0; s < spaces; s++)
        {
            result += " ";
        }
        result += wordList.get(i);
    }

    return result;
}
numGaps = wordList.size()-1. baseSpaces = total/numGaps. extraSpaces = total%numGaps. The first extraSpaces gaps each get one additional space — use a counter or conditional to track when extra spaces are exhausted.

Common Mistakes to Avoid

Not distributing the remainder to leftmost gaps

Wrong

// Gives every gap the same number of spaces — wrong when total%numGaps != 0

Correct

int spaces = baseSpaces + (gapIndex < extraSpaces ? 1 : 0); // correct

Integer division truncates. The remainder (total % numGaps) must be distributed one extra space per gap, starting from the left, until the remainder is used up.

Using wordList.size() instead of wordList.size()-1 for gap count

Wrong

int numGaps = wordList.size(); // off by one

Correct

int numGaps = wordList.size() - 1; // correct: n words have n-1 gaps

n words have exactly n-1 gaps between them. Using n as the gap count divides the spaces too thinly.

Exam Tips

Part C is worth 5 points — most of the FRQ score. Spend time here.
Must call leftoverSpaces() (and through it, totalLetters()) to earn dependency points.
Build the result string by starting with wordList.get(0), then for each subsequent word, appending spaces then the word.

Frequently Asked Questions

What does 2016 AP CSA FRQ 4 StringFormatter ask?

It asks students to write three static methods: totalLetters() (sum of word lengths), leftoverSpaces() (spaces needed to fill the line), and format() (distribute spaces evenly with leftmost gaps getting extra space when the total is not evenly divisible).

What is the hardest part of format()?

Correctly distributing the remainder spaces to the leftmost gaps. The formula is: each of the first (total % numGaps) gaps gets one extra space on top of the base (total / numGaps) spaces. The remainder must be tracked and decremented as gaps are filled left-to-right.

See All 2016 AP CSA FRQs

View the complete 2016 exam hub with solutions, difficulty analysis, and scoring for all four questions.

View 2016 FRQ Hub →

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]