Skip to main content
Logo image

Section 15.6 FRQ 1B - Strings - Part 3

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:
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.

Subsection 15.6.1 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.

Subsection 15.6.2 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.
Figure 15.6.1. 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!").

Activity 15.6.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]".

Subsection 15.6.3 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.
Figure 15.6.2. 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 */

    }
}

Activity 15.6.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.

Subsection 15.6.4 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 */
}
Figure 15.6.3. Sample Output
Constraints:
  • If the abbreviation is not found, the original message should be returned.
  • The search should be case-sensitive.

Activity 15.6.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.
You have attempted of activities on this page.