AP CSA FRQ Guide
2017 FRQ 3: Phrase
Modify a Phrase class by replacing a specific occurrence of a target string and finding the last occurrence of a target.
Skills Tested
What this FRQ is really practicing
Review these topics before attempting the problem.
Starter Approach
How to begin with your own plan
- 1Part A: call the provided findNthOccurrence(str, n) helper and update the phrase only when the returned index is not -1.
- 2Part B: call findNthOccurrence with increasing occurrence numbers, preserving the last valid index and returning -1 when no occurrence exists.
Attempt the problem before opening the solution.
Common Mistakes
Mistakes to watch for while writing
Self Check
Review questions before you submit
Uses the provided findNthOccurrence helper.
Both parts require findNthOccurrence calls for full credit; do not reimplement it with indexOf.
Tracks which occurrence has been found.
The nth occurrence is not always the first occurrence.
Rebuilds the phrase using substring pieces.
Strings are immutable, so replacement must create a new string.
Handles missing occurrences safely.
findNthOccurrence returns -1 when that occurrence does not exist.
Practice Links
