
String manipulation is one of the most heavily tested concepts in the AP Computer Science A FRQ section. Students must know how to process characters, analyze substrings, validate formats, and build new strings using loops and conditions.
This page provides exam-style FRQ practice questions designed to strengthen your string-processing logic.
Why Practice String FRQs?
AP CSA often includes free-response problems requiring:
- substring extraction
- counting patterns
- validating input formats
- scanning through characters
- building new strings with rules
- solving text-based algorithm problems
Mastering these string skills dramatically improves FRQ performance.
How to Use This Page
- Attempt each problem before checking the solution
- Compare your logic with the sample answer
- Study the Common Mistakes to avoid scoring loss
- Re-practice the problem until you can solve it quickly
- Use these as real exam warm-ups
FRQ 1 — Count Vowels (Easy)
Problem:
Write a method countVowels(String s) that returns how many vowels (a, e, i, o, u) appear in the string. Ignore case.
Example:
Input: "Computer" → Output: 3
Approach:
- Convert string to lowercase
- Loop through each character
- Check whether the character is in
"aeiou"
Java Solution:
public int countVowels(String s) {
String low = s.toLowerCase();
int count = 0;
for (int i = 0; i < low.length(); i++) {
char c = low.charAt(i);
if ("aeiou".indexOf(c) != -1) {
count++;
}
}
return count;
}
Common Mistakes:
- Forgetting uppercase vowels
- Using == to compare strings
- Checking only one vowel
FRQ 2 — Remove All Digits (Medium)
Problem:
Write a method removeDigits(String s) that returns a new string with all digits removed.
Example:
Input: "a1b2c3d4" → Output: "abcd"
Approach:
- Loop through the string
- Append non-digit characters only
- Use
Character.isDigit()
Java Solution:
public String removeDigits(String s) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (!Character.isDigit(c)) {
result.append(c);
}
}
return result.toString();
}
Common Mistakes:
- Rebuilding string with + in loop (slower)
- Returning null for empty result
- Incorrect digit checks
FRQ 3 — Find the Longest Word (Medium)
Problem:
Write a method longestWord(String sentence) that returns the longest word from a sentence.
Assume words are separated by spaces.
Example:
Input: "I love programming in Java" → Output: "programming"
Approach:
- Split using
split(" ") - Track the longest word
- Return longest
Java Solution:
public String longestWord(String sentence) {
String[] words = sentence.split(" ");
String longest = "";
for (String w : words) {
if (w.length() > longest.length()) {
longest = w;
}
}
return longest;
}
Common Mistakes:
- Not handling multiple spaces
- Using >= causing different tie results
- Not returning “” when sentence is empty
FRQ 4 — Count Substring Occurrences (Hard)
Problem:
Write a method countOccurrences(String s, String sub) that returns how many times sub appears in s.
Overlapping occurrences must be counted.
Example:
Input: "aaaa" and "aa" → Output: 3
Approach:
- Loop from 0 to
s.length() - sub.length() - Compare using
.equals() - Move window by 1
Java Solution:
public int countOccurrences(String s, String sub) {
int count = 0;
for (int i = 0; i <= s.length() - sub.length(); i++) {
if (s.substring(i, i + sub.length()).equals(sub)) {
count++;
}
}
return count;
}
Common Mistakes:
- Using == for substring comparison
- Off-by-one loop errors
- Forgetting overlapping matches
FRQ 5 — Validate ID Format (Hard)
Problem:
A valid ID has the pattern:
2 letters + 4 digits
Example valid → "AB1234"
Invalid → "A1234", "ABC123" "AB12C4"
Write a method isValidID(String id) that returns true if valid.
Approach:
- Check length = 6
- First 2 chars must be letters
- Last 4 chars must be digits
Java Solution:
public boolean isValidID(String id) {
if (id.length() != 6) return false;
if (!Character.isLetter(id.charAt(0)) ||
!Character.isLetter(id.charAt(1))) {
return false;
}
for (int i = 2; i < 6; i++) {
if (!Character.isDigit(id.charAt(i))) {
return false;
}
}
return true;
}
Common Mistakes:
- Not checking length before accessing chars
- Using try/catch unnecessarily
- Returning true even when first letters are lowercase/uppercase mismatch
More String FRQs Coming Soon
- Reverse each word
- Check for palindrome
- Remove duplicate characters
- Run-length encoding (compression)
- Extract balanced parentheses
- Pattern-based filtering tasks
Related FRQ Pages
- Arrays – FRQ Practice
- ArrayList – FRQ Practice
- 2D Arrays – FRQ Practice
- Classes & Objects – FRQ Practice
- Mixed FRQs
- Mock FRQ Sets
(Add links once pages are published.)
Need Help With AP CSA?
Get personalized AP Computer Science A tutoring from a Java expert with 15+ years of experience.
- 1-on-1 Zoom sessions
- Full FRQ + MCQ preparation
- Doubt clearing support
- Structured topic-wise guidance
WhatsApp: +91 9853166385
Book a free demo at javatutoronline.com/contact-us/