AP CSA 2017 FRQ 3 Phrase

FRQ Archive2017 FRQs › FRQ 3: Phrase
2017 AP CSA • String Manipulation

AP CSA 2017 FRQ 3: Phrase

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

9 Points Medium-Hard String Manipulation Classic Format (Pre-2019)
Question Type String Manipulation
Key Skills Implement findNthOccurrence() (find the nth occurrence of a target string using repeated indexOf() calls that advance fr
Difficulty Medium-Hard
AP CSA Units Unit 2: Selection & Iteration (loop with String search), Unit 1 (String methods: indexOf(), substring(), length()).

What This Problem Asks

Implement findNthOccurrence() (find the nth occurrence of a target string using repeated indexOf() calls that advance fromIndex each time) and replaceNthOccurrence() (replace that occurrence), then implement findLastOccurrence() by composing both.

Official Question & PDF

Download Full 2017 FRQ PDF →

PDF cannot display here. Open PDF directly →

Provided Code

public class Phrase
{
    private String currentPhrase;

        public Phrase(String p) {
        currentPhrase = p;
    }

    public int findNthOccurrence(String str, int n)
    { /* to be implemented in part (a) */ }

    public void replaceNthOccurrence(String str, int n, String repl)
    { /* to be implemented in part (b) */ }

    public int findLastOccurrence(String str)
    { /* to be implemented in part (c) */ }

        public String toString() {
        return currentPhrase;
    }
}
Timer 22:00

Part A — findNthOccurrence (5 Points)

Write findNthOccurrence(). Return the index of the nth occurrence of str in currentPhrase, or -1 if fewer than n occurrences exist.

Drag corner to expand ▽

Scoring Rubric

+1 Uses indexOf() with a fromIndex parameter
+1 Loops n times (or counts occurrences)
+1 Advances fromIndex past the previous match
+1 Returns -1 when fewer than n occurrences found
+1 Returns correct index (algorithm)

Solution

public int findNthOccurrence(String str, int n)
{
    int index = currentPhrase.indexOf(str);
    int count = 1;

    while (index != -1 && count < n)
    {
        index = currentPhrase.indexOf(str, index + 1);
        count++;
    }

    if (count == n)
        return index;
    return -1;
}
Start with the first occurrence, then advance fromIndex by 1 past each found position on each loop iteration. Stop when you've found the nth one or run out of occurrences.

Part B — replaceNthOccurrence (2 Points)

Write replaceNthOccurrence(). Replace the nth occurrence of str with repl in currentPhrase. If fewer than n occurrences exist, currentPhrase is unchanged.

Drag corner to expand ▽

Scoring Rubric

+1 Calls findNthOccurrence() to get the index
+1 Replaces the correct occurrence using substring() (algorithm)

Solution

public void replaceNthOccurrence(String str, int n, String repl)
{
    int index = findNthOccurrence(str, n);

    if (index != -1)
    {
        currentPhrase = currentPhrase.substring(0, index)
                      + repl
                      + currentPhrase.substring(index + str.length());
    }
}
The rubric explicitly requires calling findNthOccurrence(). Reimplementing the search logic instead of calling the helper loses points. Use substring() for the string replacement.

Part C — findLastOccurrence (2 Points)

Write findLastOccurrence(). Return the index of the last occurrence of str in currentPhrase, or -1 if it does not appear.

Drag corner to expand ▽

Scoring Rubric

+1 Calls findNthOccurrence() or replaceNthOccurrence() (must use prior methods)
+1 Returns index of last occurrence, -1 if none (algorithm)

Solution

public int findLastOccurrence(String str)
{
    int index = -1;
    int next = findNthOccurrence(str, 1);
    int n = 1;

    while (next != -1)
    {
        index = next;
        n++;
        next = findNthOccurrence(str, n);
    }

    return index;
}
Loop using findNthOccurrence(str, 1), (str, 2), etc., keeping track of the last successful result. When findNthOccurrence returns -1, the previous successful return value is the last occurrence.

Common Mistakes to Avoid

Not advancing fromIndex past the match

Wrong

index = currentPhrase.indexOf(str, index); // infinite loop if str is found

Correct

index = currentPhrase.indexOf(str, index + 1); // advance past match

If fromIndex doesn't advance past the current match, indexOf() finds the same occurrence every iteration, producing an infinite loop.

Not calling findNthOccurrence() in parts B and C

Wrong

// Reimplementing the search from scratch in replaceNthOccurrence

Correct

int index = findNthOccurrence(str, n); // required by rubric

Parts B and C explicitly require using findNthOccurrence(). Reimplementing the logic without calling the helper loses the dependency point.

Exam Tips

Part A is worth 5 points — spend most of your time here.
The fromIndex in indexOf(str, fromIndex) must advance by 1 past the found index, not by str.length(), to handle overlapping patterns.
For Part C, call findNthOccurrence() with increasing n values until it returns -1.

Frequently Asked Questions

What does 2017 AP CSA FRQ 3 Phrase ask?

It asks students to implement three String manipulation methods: findNthOccurrence() using repeated indexOf() calls, replaceNthOccurrence() using the helper, and findLastOccurrence() composing both — demonstrating method decomposition and String traversal.

What is the most common mistake on FRQ 3 Phrase?

In findNthOccurrence(), not advancing fromIndex past the current match (index + 1), which causes an infinite loop. In parts B and C, reimplementing the search instead of calling findNthOccurrence() as required, losing the dependency rubric point.

See All 2017 AP CSA FRQs

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

View 2017 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]