FRQ 1B - Strings - Part 2
From CS Awesome 2CC BY-NC-SA 4.0Original page
Java1 class period4 exercises20 graded tests
Files
FRQ 1B - Strings - Part 2
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.
String Algorithms
There are many algorithms that traverse or process Strings that use loops and conditional statements.
Common algorithms include:
- Counting the number of substrings that meet specific criteria
- Determining if one or more substrings has a specific property
- Building a new String based on certain conditions
Processing a String with a For Loop
Many algorithms can be solved with the accumulator pattern, similar to FRQ1A. In some cases, the accumulator value will be a String.
There are four steps to the accumulator pattern:
- initialize the accumulator variable before the loop
- loop through the letters of the String
- update the accumulator variable inside the loop
- return the accumulated value (if necessary)
String Algorithm - Count Vowels
Exercise 1
You are working on a text-analysis tool. The StringProcessor class contains a helper method, isVowel(String letter), which returns true if a single-character string is a vowel (A, E, I, O, U, case-insensitive) and false otherwise.
Write the countVowels method. This method analyzes a given String and returns the total number of vowels present in that string.
Open the
01-frq1b2-countVowelsfolder and editStringProcessor.java. Press Run to try it; Run Tests checks it (5 tests).
String Algorithm - Make Abbreviation
Exercise 2
Write a method called makeAbbreviation that takes a multi-word String and returns a new String consisting of the first letter of each word in uppercase.
Open the
02-frq1b2-makeAbbreviationfolder and editAbbreviator.java. Press Run to try it; Run Tests checks it (5 tests).
String Algorithm - Mask Vowels
Exercise 3
Write a method called maskVowels that takes a String and returns a new String where all vowels are replaced with asterisks (*), but only if the String is longer than 3 letters.
Open the
03-frq1b2-maskvowelsfolder and editWordTransformer.java. Press Run to try it; Run Tests checks it (5 tests).
String Algorithm - Count Occurrences
Exercise 4
Write a method called countOccurrences that determines how many times a specific target string appears within a larger text string.
Be careful! It is easy to end up with an infinite loop if you are not careful when updating your loop variable. You will need to use the indexOf method to find the next occurrence of the target string, and then update your loop variable to start searching for the next occurrence after the current one.
Open the
04-frq1b2-countOccurrencesfolder and editPhraseAnalyzer.java. Press Run to try it; Run Tests checks it (5 tests).
For teachers
FRQ 1B - Strings - Part 2
Students read CSAwesome2's “FRQ 1B - Strings - Part 2” in CS Room and work through its 4 Java coding exercises.
- Time: 1 class period
- Tests: 20 in 4 graded exercises, set up for you when you import the lesson
Goals
- Practice for the AP exam's free-response questions.
Before class
- Import the lesson into your class from the Lessons page, or with New assignment → Use a lesson. It arrives as a draft with its tests already set up.
- Publish it.
- The original page is on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/FRQ1B-Part2.html
- CS Awesome's own teacher materials: the CS Awesome teacher site
In class
- Students read
README.md. - Students do exercises 01–04, each in its own folder, pressing Run to try their code.
- 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
| Folder | File | Grading | Tests |
|---|---|---|---|
01-frq1b2-countVowels | StringProcessor.java | Autograded | 5 |
02-frq1b2-makeAbbreviation | Abbreviator.java | Autograded | 5 |
03-frq1b2-maskvowels | WordTransformer.java | Autograded | 5 |
04-frq1b2-countOccurrences | PhraseAnalyzer.java | Autograded | 5 |
Teachers can download a reference solution after signing in.