AP CSA 2015 FRQ 2 Hiddenword

FRQ Archive2015 FRQs › FRQ 2: HiddenWord
2015 AP CSA • Class Writing

AP CSA 2015 FRQ 2: HiddenWord

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

9 Points Medium Class Writing Classic Format (Pre-2019)
Question Type Class Writing
Key Skills Write the complete HiddenWord class for a word-guessing game. The getHint() method returns a hint string: '+' for a lett
Difficulty Medium
AP CSA Units Unit 3: Class Creation (complete class, constructor) and Unit 1 (String methods: charAt(), indexOf(), length()).

What This Problem Asks

Write the complete HiddenWord class for a word-guessing game. The getHint() method returns a hint string: '+' for a letter in the correct position, '*' for a letter present in the word but wrong position, '?' for a letter not in the word at all.

Official Question & PDF

Download Full 2015 FRQ PDF →

PDF cannot display here. Open PDF directly →

Provided Code

// Write the entire HiddenWord class
// It must have: private String hidden, a constructor, and getHint(String guess)
Timer 22:00

Entire HiddenWord Class (9 Points)

Write the complete HiddenWord class with a constructor that stores the hidden word and a getHint() method that builds and returns the hint string.

Drag corner to expand ▽

Scoring Rubric

+1 Declares private instance variable for the hidden word
+1 Constructor stores the hidden word
+1 getHint() loops over all positions of guess
+1 Correct '+' for exact position match
+1 Correct '*' for letter in word but wrong position
+1 Correct '?' for letter not in word
+1 Builds hint string of same length as guess
+1 Handles all three cases correctly together (no overlap errors)
+1 Algorithm: returns correct hint string

Solution

public class HiddenWord
{
    private String hidden;

    public HiddenWord(String word)
    {
        hidden = word;
    }

    public String getHint(String guess)
    {
        String hint = "";

        for (int i = 0; i < guess.length(); i++)
        {
            String letter = guess.substring(i, i + 1);

            if (letter.equals(hidden.substring(i, i + 1)))
            {
                hint += "+";
            }
            else if (hidden.indexOf(letter) >= 0)
            {
                hint += "*";
            }
            else
            {
                hint += "?";
            }
        }

        return hint;
    }
}
Check the exact position first ('+'), then check if the letter appears anywhere in the hidden word ('*'), and only use '?' if neither applies. Use String.indexOf() to check presence, or charAt() with == for char comparison.

Common Mistakes to Avoid

Checking '*' before '+' (wrong priority)

Wrong

if (hidden.contains(letter)) {
    hint += '*'; // marks exact match as *
}

Correct

if (letter.equals(hidden.substring(i,i+1))) {
    hint += '+'; // check exact first
}

Always check for exact position match ('+') first. If you check for general presence ('*') first, exact matches get marked as '*' instead of '+'.

Using == for char comparison instead of .equals() for String

Wrong

if (guess.charAt(i) == hidden.charAt(i)) // works for char but be careful

Correct

if (letter.equals(hidden.substring(i, i+1))) // String comparison — safer

If using charAt() (which returns char), == is correct. If using substring() (which returns String), use .equals(). Mixing them produces compilation errors or incorrect results.

Exam Tips

The three cases are mutually exclusive and must be checked in order: exact match (+), present elsewhere (*), absent (?).
String.indexOf(letter) returns -1 if not found, >= 0 if found anywhere. This handles the '*' check cleanly.
The hint has the same length as the guess word — your loop runs guess.length() times.

Frequently Asked Questions

What does 2015 AP CSA FRQ 2 HiddenWord ask?

It asks students to write a complete HiddenWord class with a constructor and getHint() method. getHint() returns a hint string where '+' means correct position, '*' means letter is in the word at a different position, and '?' means letter is not in the word.

What is the most common mistake on HiddenWord?

Checking whether the letter is anywhere in the hidden word before checking if it's in the exact position. This causes exact matches to be reported as '*' instead of '+'. Always check for exact position match first.

See All 2015 AP CSA FRQs

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

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