Ap Csa 2021 Frq 1 Wordmatch

FRQ Archive2021 FRQs › FRQ 1: WordMatch
2021 AP CSA • Methods & Control Structures

AP CSA 2021 FRQ 1: WordMatch

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

9 Points Medium Units 1 & 2
Question Type Methods & Control Structures
Skills Tested String substring/equals/compareTo, overlapping occurrence counting, running max with tie-break
Difficulty Medium
Recommended Time 22 minutes

What This Problem Asks

2021 AP CSA FRQ 1 WordMatch asks students to write two methods for a word-matching game. scoreGuess counts how many times guess appears as a (possibly overlapping) substring of secret and multiplies by guess.length() squared. findBetterGuess returns the higher-scoring guess; on a tie, it returns the alphabetically greater one using compareTo.

What This FRQ Tests

This FRQ tests Unit 1: String methods (substring, equals, compareTo, length) and Unit 2 (loops, conditionals).

Official Question & PDF

Download Full FRQ PDF →

PDF cannot display on this device. Open PDF directly →

Timer 22:00

Provided Code

public class WordMatch
{
    private String secret;
    public WordMatch(String word) { /* not shown */ }
    /** Returns count of overlapping occurrences of guess in secret, multiplied by guess.length()^2. */
    public int scoreGuess(String guess) { /* to be implemented in part (a) */ }
    /** Returns the better of two guesses: higher score wins; if tied, alphabetically greater wins. */
    public String findBetterGuess(String guess1, String guess2) { /* to be implemented in part (b) */ }
}

Part A — 4 Points

Write scoreGuess: count overlapping occurrences of guess in secret and return the count multiplied by guess.length() * guess.length().

Write Your Solution

Drag corner to expand ▽

Scoring Rubric (Part A — 4 points)

+1 Loops through substrings of secret of length guess.length()
+1 Uses substring correctly to extract substrings of the right length
+1 Counts occurrences using equals or compareTo
+1 Returns count multiplied by guess.length() * guess.length()

Solution

public int scoreGuess(String guess)
{
    int count = 0;
    int gLen = guess.length();
    for (int i = 0; i <= secret.length() - gLen; i++)
    {
        if (secret.substring(i, i + gLen).equals(guess))
        {
            count++;
        }
    }
    return count * gLen * gLen;
}
Overlapping substrings: The loop increments by 1 each time, allowing overlapping matches. For secret = "aaaabb" and guess = "aa", positions 0, 1, and 2 all match — that's 3 occurrences. The loop bound is <= secret.length() - gLen to avoid going out of bounds.

Part B — 5 Points

Write findBetterGuess: return the guess with the higher score. On a tie, return the alphabetically greater guess. Must use scoreGuess.

Write Your Solution

Drag corner to expand ▽

Scoring Rubric (Part B — 5 points)

+1 Calls scoreGuess for both guesses
+1 Compares the two scores
+1 Returns the guess with the higher score when scores differ
+1 Handles the tie-breaking case (alphabetically greater)
+1 Returns the correct value in all cases (algorithm)

Solution

public String findBetterGuess(String guess1, String guess2)
{
    int score1 = scoreGuess(guess1);
    int score2 = scoreGuess(guess2);
    if (score1 > score2) {
        return guess1;
    }
    if (score2 > score1) {
        return guess2;
    }
    // tie: return alphabetically greater
    if (guess1.compareTo(guess2) > 0) {
        return guess1;
    }
    return guess2;
}
Alphabetically greater: compareTo returns a positive value when guess1 comes AFTER guess2 alphabetically. "con" > "cat" because 'o' comes after 'a'. The precondition guarantees the guesses are not equal, so exactly one will be alphabetically greater.

Common Mistakes to Avoid

Incorrect loop bound for substring extraction

The loop must stop at secret.length() - gLen (inclusive) to avoid a StringIndexOutOfBoundsException.

Wrong

for (int i = 0; i < secret.length(); i++)  // too far!

Correct

for (int i = 0; i <= secret.length() - gLen; i++)
Using == instead of .equals() for String comparison

Wrong

if (secret.substring(i, i + gLen) == guess)

Correct

if (secret.substring(i, i + gLen).equals(guess))

Exam Tips

The loop bound i <= secret.length() - guess.length() is crucial. Going too far causes StringIndexOutOfBoundsException.
Part B says "Assume scoreGuess works as intended." Use it — the rubric requires calling scoreGuess.
compareTo returns positive if the calling string is alphabetically greater. "con".compareTo("cat") > 0, so "con" is returned as the alphabetically greater string.

Scoring Summary

Part Method Points
Part A Write 4
Part B Write 5
Total 9

Want the Complete 2021 FRQ Solutions?

Browse all past AP CSA FRQs with full solutions and scoring breakdowns.

FRQ Archive →

Related FRQs

Methods & Control Structures 2022 FRQ 1: Game — Cascading level scoring and max simulation Methods & Control Structures 2023 FRQ 1: AppointmentBook — Consecutive-block scheduling algorithm

Study the Concepts

Unit 1 Study Guide → Unit 2 Study Guide →

Struggling with FRQs? Get 1-on-1 Help

Work directly with Tanner — AP CS teacher with 11+ years experience and 1,845+ verified tutoring hours. 54.5% of students score 5s (vs. 25.5% national average).

5.0Rating (451+ reviews)
1,845+Verified Hours
54.5%Score 5s

Book a Session ($150/hr) →

5-session packages at $125/hr. Venmo, Zelle, PayPal, or credit card.

Frequently Asked Questions

What does 2021 AP CSA FRQ 1 WordMatch test?

FRQ 1 tests String methods (substring, equals, compareTo) and loop-based substring counting. scoreGuess counts overlapping occurrences of guess in secret and multiplies by the square of guess length. findBetterGuess compares scores with a tie-breaking compareTo.

How many points is 2021 AP CSA FRQ 1 worth?

9 points: 4 for Part A (scoreGuess) and 5 for Part B (findBetterGuess).

What is the correct loop bound for scoreGuess?

Use i <= secret.length() - guess.length(). Going up to secret.length() - 1 would try to take a substring that goes out of bounds for any guess longer than 1 character.

How does the alphabetical tie-breaker work?

Use guess1.compareTo(guess2). If it returns a positive value, guess1 comes after guess2 alphabetically (is 'greater'), so return guess1. The precondition guarantees the guesses are different, so compareTo will never return 0.

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]