FRQ 1B - Strings - Part 3

From CS Awesome 2CC BY-NC-SA 4.0Original page

Java1 class period3 exercises15 graded tests

Files

README.mdDownload

FRQ 1B - Strings - Part 3

Each coding exercise has its own folder. Open its .java file and press Run to try it. Run Tests checks every graded exercise in this lesson at once.

From the 2025 Course and Exam Description:

Free-Response Question 1: Methods and Control Structures. Students will write two methods or one constructor and one method of a given class based on the provided specifications and examples. In Part A (4 points), the method or constructor will require students to write iterative or conditional statements, or both, as well as statements that call methods in the specified class.

In Part B (3 points)****, the method or constructor will require calling String methods.

FRQ 1B requires the following skills:

  • Complete a method or a constructor
  • Call String methods
  • Use if statements
  • Use a for loop or a while loop

Teacher Insights

  • You do not need to include import statements. You can assume that any standard Java class is already imported.
  • You will NEVER print. If you think you need to print, you probably need to return something.
  • You will NOT need to write a main.

Note - Practice FRQs

There are very few examples of FRQ1B, since it has only existed since 2025-2026. You will see several examples of Strings being used in FRQs 2-3 in the following lessons.

Here are a few unofficial FRQs that are written more like an official FRQ.

Practice FRQ - The EmailFilter Class

A system is being designed to "sanitize" email subject lines by removing specific "flagged" words and replacing them with a placeholder. You will write the method sanitizeSubject.

The sanitizeSubject method takes two parameters: a String subject and a String flaggedWord. The method should return a new String where every occurrence of flaggedWord in subject is replaced by the string "[REDACTED]".

/**
 * Returns a version of the subject where every instance of flaggedWord
 * is replaced by "[REDACTED]".
 * Precondition: subject and flaggedWord are not null.
 */
public String sanitizeSubject(String subject, String flaggedWord) {

    // TO BE IMPLEMENTED IN PART (B)

}

Constraints and Rules:

  • If the flaggedWord is not found, the original subject should be returned.
  • The search should be case-sensitive.

Sample Output

Sample Output

To solve this, you need a while loop to find the word, and you must "snip" the string into pieces to reassemble it.

Common AP Exam Traps to Avoid:

  • The Infinite Loop: If you don't update the string you are searching (or don't move your index), the indexOf will keep finding the same word forever.
  • The "Last Bit" Bug: Many students forget to add result += remaining at the very end. Without it, your code would cut off the end of the sentence (e.g., "Check out this [REDACTED]" instead of "Check out this [REDACTED] offer!").
Exercise 1

A system is being designed to "sanitize" email subject lines by removing specific "flagged" words and replacing them with a placeholder. You will write the method sanitizeSubject.

The sanitizeSubject method takes two parameters: a String subject and a String flaggedWord. The method should return a new String where every occurrence of flaggedWord in subject is replaced by the string "[REDACTED]".

Open the 01-frq1b3-EmailFilter folder and edit EmailFilter.java. Press Run to try it; Run Tests checks it (5 tests).

Practice FRQ - The SequenceAnalyzer Class

In bioinformatics, scientists analyze DNA strings composed of the characters A, C, G, and T. A "doublet" is defined as any two-character sequence where a character is immediately followed by the same character (e.g., "AA", "GG").

Write the method countValidDoublets. This method analyzes a given DNA string and counts how many doublets exist, but only if the doublet appears after the first occurrence of a "start marker" character.

Sample Output

Sample Output

public class SequenceAnalyzer {
    /**
     * Counts doublets (two identical consecutive characters) that
     * appear AFTER the first occurrence of startMarker.
     * Precondition: dna is not null, dna.length() >= 2.
     */
    public static int countValidDoublets(String dna, String startMarker) {

        /* TO BE IMPLEMENTED */

    }
}
Exercise 2

In bioinformatics, scientists analyze DNA strings composed of the characters A, C, G, and T. A "doublet" is defined as any two-character sequence where a character is immediately followed by the same character (e.g., "AA", "GG").

Write the method countValidDoublets. This method analyzes a given DNA string and counts how many doublets exist, but only if the doublet appears after the first occurrence of a "start marker" character.

Open the 02-frq1b3-sequenceAnalyzer folder and edit SequenceAnalyzer.java. Press Run to try it; Run Tests checks it (5 tests).

Practice FRQ - TextExpander

A software company is creating a "Short-Hand Expander" that converts common abbreviations into their full-word equivalents. You will write a method for the TextExpander class that finds and replaces specific abbreviations.

Write the method expandText, which takes three String parameters: message, abbr, and fullWord. The method should return a new String where every occurrence of the abbreviation abbr in message is replaced by fullWord.

/**
 * Returns a version of message where every instance of abbr
 * is replaced by fullWord.
 * Precondition: message, abbr, and fullWord are not null.
 */
public String expandText(String message, String abbr, String fullWord) {

    /* TO BE IMPLEMENTED BELOW */
}

Sample Output

Sample Output

Constraints:

  • If the abbreviation is not found, the original message should be returned.
  • The search should be case-sensitive.
Exercise 3

A software company is creating a "Short-Hand Expander" that converts common abbreviations into their full-word equivalents. You will write a method for the TextExpander class that finds and replaces specific abbreviations.

Write the method expandText, which takes three String parameters: message, abbr, and fullWord. The method should return a new String where every occurrence of the abbreviation abbr in message is replaced by fullWord.

Open the 03-frq1b3-TextExpander folder and edit TextExpander.java. Press Run to try it; Run Tests checks it (5 tests).

For teachers

FRQ 1B - Strings - Part 3

Students read CSAwesome2's “FRQ 1B - Strings - Part 3” in CS Room and work through its 3 Java coding exercises.

  • Time: 1 class period
  • Tests: 15 in 3 graded exercises, set up for you when you import the lesson

Goals

  • Practice for the AP exam's free-response questions.

Before class

In class

  1. Students read README.md.
  2. Students do exercises 01–03, each in its own folder, pressing Run to try their code.
  3. Run Tests checks every graded exercise at once; scores appear in the Gradebook. Grading uses your copy of the tests, so a student who edits theirs does not change the grade.

Exercises

FolderFileGradingTests
01-frq1b3-EmailFilterEmailFilter.javaAutograded5
02-frq1b3-sequenceAnalyzerSequenceAnalyzer.javaAutograded5
03-frq1b3-TextExpanderTextExpander.javaAutograded5

Teachers can download a reference solution after signing in.