AP CSA 2017 FRQ 3 Phrase
AP CSA 2017 FRQ 3: Phrase
Complete solution, scoring rubric, and walkthrough — verified from the official College Board PDF
| 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
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;
}
}
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.
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;
}
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.
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());
}
}
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.
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;
}
Common Mistakes to Avoid
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.
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
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.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]