AP CSA 2016 FRQ 4 Stringformatter
AP CSA 2016 FRQ 4: StringFormatter
Complete solution, scoring rubric, and walkthrough — verified from the official College Board PDF
| 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
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) */ }
}
Part A — totalLetters (2 Points)
Write totalLetters(). Return the total number of characters across all words in wordList.
Scoring Rubric
| +1 | Iterates over wordList |
| +1 | Accumulates word lengths and returns the sum |
Solution
public static int totalLetters(ArrayListwordList) { int total = 0; for (String word : wordList) { total += word.length(); } return total; }
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.
Scoring Rubric
| +1 | Calls totalLetters() to get character count |
| +1 | Returns formattedLen minus totalLetters() result |
Solution
public static int leftoverSpaces(ArrayListwordList, int formattedLen) { return formattedLen - totalLetters(wordList); }
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.
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(ArrayListwordList, 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; }
Common Mistakes to Avoid
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.
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
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.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]