AP CSA 2018 FRQ 3: CodeWordChecker | Complete Solution & Rubric

FRQ Archive2018 FRQs › FRQ 3: CodeWordChecker
2018 AP CSA • Interface Implementation

AP CSA 2018 FRQ 3: CodeWordChecker

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

9 Points Medium-Hard Interface Design Classic Format (Pre-2019)
Note: This FRQ tests interface implementation with implements and StringChecker, which was removed from the AP CSA curriculum after 2018. The current exam does not test interfaces or inheritance. The core class-writing skills (constructors, fields, instance methods) are fully applicable today.
Question Type Interface Implementation (Class Writing)
Interface StringChecker — single method: boolean isValid(String str)
Skills Tested Two constructors, default field values, String.contains(), length bounds check
Difficulty Medium-Hard
Recommended Time 22 minutes

What This Problem Asks

2018 AP CSA FRQ 3 CodeWordChecker asks students to write a complete class that implements the StringChecker interface. A CodeWordChecker validates strings against two criteria: the string must fall within a specified length range, and it must not contain a specified forbidden substring.

The class must support two constructors: a three-argument version specifying minimum length, maximum length, and forbidden string; and a one-argument version that takes only the forbidden string and uses default bounds of 6 (min) and 20 (max).

Official Question & PDF

Download Full 2018 FRQ PDF →

PDF cannot display on this device. Open PDF directly →

The Problem

The StringChecker interface is provided:

public interface StringChecker
{
    /** Returns true if str is valid. */
    boolean isValid(String str);
}

A CodeWordChecker implements StringChecker. It can be constructed two ways:

  • Three-argument: new CodeWordChecker(5, 8, "$") — valid words have 5–8 characters and must not contain "$"
  • One-argument: new CodeWordChecker("pass") — valid words must not contain "pass"; length bounds default to 6 and 20

Example behavior:

Method Call Returns Reason
sc1.isValid("happy") true 5 chars, no "$"
sc1.isValid("happy$") false contains "$"
sc1.isValid("Code") false only 4 chars (too short)
sc1.isValid("happyCode") false 9 chars (too long)
sc2.isValid("MyPass") true 6 chars, no "pass" (case-sensitive)
sc2.isValid("Mypassport") false contains "pass"
sc2.isValid("happy") false only 5 chars (default min is 6)
Timer 22:00

Write Your Solution

Drag corner to expand ▽

Scoring Rubric (9 points total)

Points Rubric Item
+1 Declares class with implements StringChecker
+1 Declares appropriate private instance variables (at least min, max, forbidden)
+1 Three-argument constructor sets all three fields correctly
+1 One-argument constructor sets forbidden field; defaults min to 6 and max to 20
+1 isValid header matches interface signature exactly: public boolean isValid(String str)
+1 Checks lower bound: str.length() >= minLength
+1 Checks upper bound: str.length() <= maxLength
+1 Checks forbidden substring: !str.contains(forbidden)
+1 Returns correct boolean combining all three conditions (algorithm)

Complete Solution

public class CodeWordChecker implements StringChecker
{
    private int minLength;
    private int maxLength;
    private String forbidden;

    // Three-argument constructor
    public CodeWordChecker(int min, int max, String str)
    {
        minLength = min;
        maxLength = max;
        forbidden = str;
    }

    // Single-argument constructor — defaults to 6 and 20
    public CodeWordChecker(String str)
    {
        minLength = 6;
        maxLength = 20;
        forbidden = str;
    }

    /** Returns true if str is a valid code word. */
    public boolean isValid(String str)
    {
        return str.length() >= minLength
            && str.length() <= maxLength
            && !str.contains(forbidden);
    }
}
Key insight: All three conditions must be true for isValid to return true: length >= min AND length <= max AND string does not contain the forbidden substring. Use String.contains(), not equals() or indexOf().

Common Mistakes to Avoid

Using equals() instead of contains() for the forbidden substring

Wrong

!str.equals(forbidden)

Correct

!str.contains(forbidden)

equals() checks if the entire string IS the forbidden word. contains() checks if it appears anywhere inside — which is what the problem requires.

Forgetting the default values in the one-argument constructor

Wrong

public CodeWordChecker(String str) {
    forbidden = str;
}

Correct

public CodeWordChecker(String str) {
    minLength = 6; maxLength = 20; forbidden = str;
}

If minLength and maxLength are instance variables (not initialized elsewhere), forgetting to set them in the one-argument constructor leaves them as 0, making the length check always pass.

Off-by-one on length bounds (< vs <=)

Wrong

str.length() > minLength && str.length() < maxLength

Correct

str.length() >= minLength && str.length() <= maxLength

The bounds are inclusive. A string of exactly minLength characters is valid. Using strict inequality rejects valid strings at the boundaries.

Not declaring implements StringChecker

The class declaration must include implements StringChecker. Without it, CodeWordChecker is not a StringChecker and cannot be assigned to a StringChecker variable — the problem's whole premise fails.

Exam Tips

Write both constructors first, then isValid. The one-argument constructor is a quick win — just assign the forbidden string and set the two defaults.
Remember: this FRQ tests interface implementation. Always write implements StringChecker in the class header. Forgetting this loses an easy rubric point.
The isValid method header must match the interface exactly: public boolean isValid(String str). Any mismatch means the class doesn’t actually implement the interface.
Current exam note: Interface implementation is no longer tested on AP CSA. If you’re studying for the current exam, focus on the constructor and field patterns — those appear every year in FRQ 2 (Class Writing).

Scoring Summary

Item Condition Points
Class declaration implements StringChecker 1
Instance variables Correct private fields 1
3-arg constructor Sets all three fields 1
1-arg constructor Sets forbidden; defaults 6 and 20 1
isValid header Matches interface exactly 1
Lower bound check >= minLength 1
Upper bound check <= maxLength 1
Forbidden check !str.contains(forbidden) 1
Algorithm Correct boolean return 1
Total 9

Frequently Asked Questions

What does 2018 AP CSA FRQ 3 CodeWordChecker ask?

It asks students to write a complete CodeWordChecker class implementing the StringChecker interface, with two constructors and an isValid method that checks length bounds and absence of a forbidden substring.

Is interface implementation still tested on the current AP CSA exam?

No. Interfaces and inheritance were removed from the AP CSA curriculum in the 2019 redesign. The current exam does not test implements, extends, or super(). This FRQ is useful practice for class writing and String methods, but the interface pattern is not testable today.

What is the most common mistake?

Using equals() instead of contains() for the forbidden substring, using strict inequalities (< and >) instead of inclusive ones (<= and >=), and forgetting to set the default min/max values in the one-argument constructor.

Practice All 2018 AP CSA FRQs

See every 2018 question with full solutions, rubrics, and difficulty analysis.

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