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.

String Search and Replacement22 minute targetStringindexOfsubstringloopsedge cases

Skills Tested

What this FRQ is really practicing

StringindexOfsubstringloopsedge cases

Treat these skills as the study checklist. If any tag feels shaky, review that topic before attempting the full written response.

Starter Approach

How to begin without copying a solution

  1. 1Part A: locate the nth occurrence using repeated searching and update the phrase only when it exists.
  2. 2Part B: search until no later occurrence exists, then return the last starting index or -1.

Write a first attempt before revealing any solution outline. Most AP CSA FRQ progress comes from tracing your own code and finding the missing case.

Common Mistakes

Mistakes to watch for while writing

Replacing the first occurrence instead of the nth occurrence.
Continuing the next search from the same index and finding the same occurrence again.
Changing the phrase when the nth occurrence does not exist.

Self Check

Review questions before you submit

Uses indexOf to locate target occurrences.

Repeated indexOf calls are the cleanest approach.

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.

indexOf returns -1 when the target is not found.

Practice Links

Where to go next