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