• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar

JavaTutorOnline

1-on-1 Online Java Training by a Senior Software Engineer

  • Home
  • AP CSA
    • FRQ Practice
      • Arrays
      • ArrayList
      • Strings
      • 2D Arrays
    • MCQ Practice
      • Booleans
      • Iteration
      • Arrays
      • View all Units
  • Courses
  • Tutorials
    • Java
    • Servlets
    • Struts
    • Spring
    • Webservice
  • FAQ
  • Testimonials
  • Blog
  • CONTACT US

Strings – FRQ Practice (AP Computer Science A)

AP Computer Science A • Unit 3 & 4 Practice

Strings – FRQ Practice (AP Computer Science A)

Strings are an important part of Java programming and frequently appear in the AP Computer Science A FRQ section. Master these 15 original AP CSA-style FRQ practice problems featuring clean Java solutions, common pitfalls to avoid, and step-by-step logic breakdowns.

⚡ 15 FRQ Practice Problems • 🎯 Easy to Hard Progression • 💡 AP CSA Java Quick Reference Methods

Students must know how to process characters, analyze substrings, validate formats, and build new strings safely using loops and conditions. Use this page as your ultimate practice hub to master the String class methods found on the AP Java Quick Reference guide.


Why Practice String FRQs?

  • Safe substring extraction and avoiding StringIndexOutOfBoundsException.
  • Counting patterns and managing sliding window boundaries.
  • Validating input formats using standard subset methods.
  • Scanning through characters using indexOf() and equals().
  • Building and accumulating new strings using concatenation.

AP CSA String Methods You Should Know

MethodWhat it does
length()Returns the number of characters in the String
substring(int, int)Returns part of a String from the first index up to (but not including) the second index
substring(int)Returns a String from an index to the end of the String
indexOf(String)Finds the first occurrence of a substring and returns its starting index
equals(Object)Compares String contents for exact equality
compareTo(String)Compares Strings lexicographically (alphabetically)
AP Computer Science A Strings FRQ Practice showing string indexing, substring, charAt, indexOf, and common FRQ tasks
AP Computer Science A Strings FRQ Practice: string indexing, common String methods, and typical FRQ patterns.

FRQ 1 — Count Vowels

Easy
Problem:

Write a method countVowels(String s) that returns the total number of vowels (a, e, i, o, u) that appear in the parameter s. Assume s is not null. Ignore case.

Example:

“Computer” → 3

Approach:
  • Traverse each character using a for loop and substring().
  • Check membership against a constant string containing both uppercase and lowercase vowels.
Java Solution
public int countVowels(String s) {
    int count = 0;
    String vowels = "aeiouAEIOU";
    for (int i = 0; i < s.length(); i++) {
        String c = s.substring(i, i + 1);
        if (vowels.indexOf(c) != -1) {
            count++;
        }
    }
    return count;
}
Common Mistakes:
  • Forgetting to check for uppercase vowels.
  • Using == instead of equals() to compare String objects.

FRQ 2 — Remove All Digits

Easy
Problem:

Write a method removeDigits(String s) that returns a new string containing all the characters of s except numerical digits.

Example:

“a1b2c3d4” → “abcd”

Approach:
  • Traverse the original string character by character.
  • Append characters to the result string only if they are not found in a master “digits” string.
Java Solution
public String removeDigits(String s) {
    String result = "";
    String digits = "0123456789";
    for (int i = 0; i < s.length(); i++) {
        String c = s.substring(i, i + 1);
        if (digits.indexOf(c) == -1) {
            result = result + c;
        }
    }
    return result;
}
Common Mistakes:
  • Returning null instead of an initialized empty string "".
  • Using non-subset methods like Character.isDigit().

FRQ 3 — Reverse a String

Easy
Problem:

Write a method reverseString(String s) that returns the string completely reversed.

Example:

“java” → “avaj”

Approach:
  • Traverse backwards from the last valid index (length() - 1) down to 0.
  • Concatenate the characters sequentially to build the reversed string.
Java Solution
public String reverseString(String s) {
    String rev = "";
    for (int i = s.length() - 1; i >= 0; i--) {
        rev = rev + s.substring(i, i + 1);
    }
    return rev;
}
Common Mistakes:
  • Starting the loop at s.length() resulting in a StringIndexOutOfBoundsException.
  • Using i > 0 instead of i >= 0 and missing the very first character.

FRQ 4 — Count Words

Easy
Problem:

Write a method countWords(String s) to return the number of words. Assume words are separated by exactly one space, there are no leading/trailing spaces, and if the string is empty it returns 0.

Example:

“AP Computer Science” → 3

Approach:
  • If the string is perfectly formatted with single spaces, the number of words will always be exactly one more than the number of spaces.
Java Solution
public int countWords(String s) {
    if (s.length() == 0) return 0;
    int count = 1;
    for (int i = 0; i < s.length(); i++) {
        if (s.substring(i, i + 1).equals(" ")) {
            count++;
        }
    }
    return count;
}
Common Mistakes:
  • Failing the empty string "" test case by returning 1 instead of 0.
  • Using == to compare substring equality with " ".

FRQ 5 — Find the Longest Word

Medium
Problem:

Write a method longestWord(String sentence) that returns the longest word. Assume words are separated by exactly one space. If there is a tie, return the first longest word found.

Example:

“I love programming in Java” → “programming”

Approach:
  • Build a current word character by character until a space is hit.
  • Compare the length of current against longest. Reset current to capture the next word.
  • Remember to process the very last word after the loop ends.
Java Solution
public String longestWord(String sentence) {
    String longest = "";
    String current = "";
    
    for (int i = 0; i < sentence.length(); i++) {
        String c = sentence.substring(i, i + 1);
        if (c.equals(" ")) {
            if (current.length() > longest.length()) {
                longest = current;
            }
            current = "";
        } else {
            current = current + c;
        }
    }
    
    // Check the final word after the last space
    if (current.length() > longest.length()) {
        longest = current;
    }
    
    return longest;
}
Common Mistakes:
  • Using >= instead of strict >, which would incorrectly return the last tie instead of the first.
  • Forgetting to do a final check for the last word outside the loop.

FRQ 6 — Remove Vowels

Medium
Problem:

Write a method removeVowels(String s) that removes all vowels from the string. Case should be ignored when determining what to remove.

Example:

“Computer” → “Cmptr”

Approach:
  • Check membership in a constant string containing uppercase and lowercase vowels via indexOf.
  • If indexOf returns -1, the character is safe to append.
Java Solution
public String removeVowels(String s) {
    String result = "";
    String vowels = "aeiouAEIOU";
    for (int i = 0; i < s.length(); i++) {
        String c = s.substring(i, i + 1);
        if (vowels.indexOf(c) == -1) {
            result = result + c;
        }
    }
    return result;
}
Common Mistakes:
  • Forgetting to list uppercase vowels in the lookup string.
  • Trying to remove characters without preserving the original order of the remaining characters.

FRQ 7 — Check Palindrome

Medium
Problem:

Write a method isPalindrome(String s) that returns true if the string reads identically forwards and backwards, and false otherwise.

Example:

“level” → true

Approach:
  • Use two index pointers: left starting at the beginning, and right starting at the end.
  • Compare characters, moving inward. If a mismatch is found, immediately return false.
Java Solution
public boolean isPalindrome(String s) {
    int left = 0;
    int right = s.length() - 1;
    while (left < right) {
        if (!s.substring(left, left + 1).equals(s.substring(right, right + 1))) {
            return false;
        }
        left++;
        right--;
    }
    return true;
}
Common Mistakes:
  • Incorrect pointer initialization (setting right to s.length() instead of s.length() - 1).
  • Using a full reverse-and-compare method, which is less efficient than the two-pointer approach.

FRQ 8 — Count Uppercase Letters

Medium
Problem:

Write a method countUppercase(String s) that returns the total count of uppercase letters in the given string.

Example:

“ApCSa” → 2

Approach:
  • For this AP CSA practice problem, we use compareTo() to determine whether each one-character String falls between “A” and “Z”.
Java Solution
public int countUppercase(String s) {
    int count = 0;
    for (int i = 0; i < s.length(); i++) {
        String c = s.substring(i, i + 1);
        if (c.compareTo("A") >= 0 && c.compareTo("Z") <= 0) {
            count++;
        }
    }
    return count;
}
Common Mistakes:
  • Counting lowercase letters instead
  • Comparing string objects using relational operators (>=) instead of compareTo().

FRQ 9 — Count Substring Occurrences

Hard
Problem:

Write a method countOccurrences(String s, String sub) that counts all (including overlapping) occurrences of sub inside s. Assume sub has a length of at least 1.

Example:

“aaaa”, “aa” → 3

Approach:
  • Slide a window across the string and compare each substring
Java Solution
public int countOccurrences(String s, String sub) {
    int count = 0;
    int subLen = sub.length();
    for (int i = 0; i <= s.length() - subLen; i++) {
        if (s.substring(i, i + subLen).equals(sub)) {
            count++;
        }
    }
    return count;
}
Common Mistakes:
  • Forgetting the <= boundary, resulting in missing a match at the very end of the string.
  • Using == instead of equals() to compare equality.

FRQ 10 — Validate ID Format

Hard
Problem:

A valid ID must be exactly 6 characters long, where the first 2 characters are strictly alphabetic letters (A-Z or a-z), followed by exactly 4 numerical digits. Return true if valid.

Example:

“AB1234” → true, “A12345” → false

Approach:
  • First evaluate the length condition. If invalid, fail early.
  • Use lookup strings to validate positions 0-1 as letters and positions 2-5 as numbers.
Java Solution
public boolean isValidID(String id) {
    if (id.length() != 6) return false;
    
    String upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    String lower = "abcdefghijklmnopqrstuvwxyz";
    String digits = "0123456789";

    String c1 = id.substring(0, 1);
    String c2 = id.substring(1, 2);

    boolean isC1Letter = (upper.indexOf(c1) != -1 || lower.indexOf(c1) != -1);
    boolean isC2Letter = (upper.indexOf(c2) != -1 || lower.indexOf(c2) != -1);

    if (!isC1Letter || !isC2Letter) {
        return false;
    }

    for (int i = 2; i < 6; i++) {
        if (digits.indexOf(id.substring(i, i + 1)) == -1) {
            return false;
        }
    }
    return true;
}
Common Mistakes:
  • Failing to enforce the exact string length prior to processing indices, causing bounds exceptions.
  • Creating overly complex, nested if logic.

FRQ 11 — Remove Extra Spaces

Hard
Problem:

Write a method removeExtraSpaces(String s). Assume the input contains no leading or trailing spaces. Replace every sequence of two or more consecutive spaces with a single space.

Example:

“AP Computer Science” → “AP Computer Science”

Approach:
  • Traverse characters and skip appending a space if the previous character was also a space
Java Solution
public String removeExtraSpaces(String s) {
    String result = "";
    for (int i = 0; i < s.length(); i++) {
        String c = s.substring(i, i + 1);
        if (i == 0 || !c.equals(" ") || !s.substring(i - 1, i).equals(" ")) {
            result = result + c;
        }
    }
    return result;
}
Common Mistakes:
  • Index out of bounds when checking previous characters

FRQ 12 — Extract Digits

Hard
Problem:

Write a method extractDigits(String s) that returns a new string containing only the numerical digits from the input string in their original order.

Example:

“ab12cd34” → “1234”

Approach:
  • Traverse each character and append only those found in a lookup digit set ("0123456789").
Java Solution
public String extractDigits(String s) {
    String result = "";
    String digits = "0123456789";
    for (int i = 0; i < s.length(); i++) {
        String c = s.substring(i, i + 1);
        if (digits.indexOf(c) != -1) {
            result = result + c;
        }
    }
    return result;
}
Common Mistakes:
  • Returning numeric types instead of a String
  • Missing characters in loops

FRQ 13 — Longest Consecutive Character

Hard
Problem:

Write a method longestStreak(String s) to return the character that has the longest consecutive repetition. Assume the string is non-empty. In the event of a tie, return the first character string to achieve that streak length.

Example:

“aaabbccccd” → “c”

Approach:
  • Track a count of the current streak, comparing curr character to prev character.
  • Track a bestCount to store the maximum streak value and update it accordingly.
Java Solution
public String longestStreak(String s) {
    if (s.length() == 0) return "";
    String best = s.substring(0, 1);
    int bestCount = 1;
    int count = 1;

    for (int i = 1; i < s.length(); i++) {
        String curr = s.substring(i, i + 1);
        String prev = s.substring(i - 1, i);
        if (curr.equals(prev)) {
            count++;
            if (count > bestCount) {
                bestCount = count;
                best = curr;
            }
        } else {
            count = 1;
        }
    }
    return best;
}
Common Mistakes:
  • Forgetting to reset the current count back to 1 when the streak ends.
  • Starting the loop at index 0 and failing bounds limits on checking i - 1.

FRQ 14 — Compress String

Hard
Problem:

Write a method compress(String s) that implements basic run-length encoding. For each group of repeating adjacent characters, append the character followed by the number of times it repeats.

Example:

“aaabbc” → “a3b2c1”

Approach:
  • Traverse the string from index 1 to s.length() - 1.
  • Count consecutive identical characters. When the sequence breaks, append the character and its count to the result.
  • Be sure to append the final character group after the loop terminates.
Java Solution
public String compress(String s) {
    if (s.length() == 0) return "";
    String result = "";
    int count = 1;

    for (int i = 1; i < s.length(); i++) {
        if (s.substring(i, i + 1).equals(s.substring(i - 1, i))) {
            count++;
        } else {
            result = result + s.substring(i - 1, i) + count;
            count = 1;
        }
    }
    // Process the final group
    result = result + s.substring(s.length() - 1) + count;
    return result;
}
Common Mistakes:
  • Forgetting to process and append the final character group after the loop finishes.

FRQ 15 — Mask Email Username

Hard
Problem:

Write a method maskEmail(String email) that masks all characters of an email username except the very first and last characters. Assume the string contains exactly one “@” and the username length is at least 1. If username length is <= 2, return the email unchanged.

Example:

“john.doe@gmail.com” → “j******e@gmail.com”

Approach:
  • Use indexOf("@") to parse the domain boundary safely.
  • Separate username and domain text pieces, loop to construct the central stars, and recombine.
Java Solution
public String maskEmail(String email) {
    int at = email.indexOf("@");
    String user = email.substring(0, at);
    String domain = email.substring(at);

    if (user.length() <= 2) return email;

    String masked = user.substring(0, 1);
    for (int i = 1; i < user.length() - 1; i++) {
        masked = masked + "*";
    }
    masked = masked + user.substring(user.length() - 1);

    return masked + domain;
}
Common Mistakes:
  • Masking the domain part of the email
  • Not accounting for short edge-case usernames.

Frequently Asked Questions

What String methods should I know for AP CSA?
You should master the methods listed on the official AP Java Quick Reference guide: length(), substring(), indexOf(), equals(), and compareTo().

How do I practice Strings for AP Computer Science A?
The best way is to write solutions by hand first, tracing your indices carefully on paper, and then type them up. Focus on loop bounds and avoiding out-of-bounds exceptions.

Are these official College Board FRQs?
No. These are original, custom-designed AP CSA–style practice problems created for additional practice. They are not released College Board questions.

Is substring() important for AP CSA?
Yes. You should understand both the single-parameter version and the two-parameter version.

How should I approach a String question on the AP CSA exam?
Read the problem carefully, clarify assumptions (like empty strings or spaces), draw out a concrete example with indices, and decide whether a for loop or a while loop is best for traversing the characters.


FRQ Strategy Tips for Strings (AP CSA)

  • Always clarify assumptions before coding.
  • Prefer index-based loops over enhanced for-loops when dealing with complex substrings.
  • Use standard String concatenation or approved subset methods.
  • Watch loop boundaries carefully to avoid StringIndexOutOfBoundsException.
  • Write clear, logically structured solutions and check each part of the question against the requirements.

Related FRQ Pages

  • Arrays – FRQ Practice
  • ArrayList – FRQ Practice
  • 2D Arrays – FRQ Practice
  • Classes & Objects – FRQ Practice
  • Mixed FRQs
  • Mock FRQ Sets

Need Structured Help With AP Computer Science A?

If you want guided, one-on-one support for AP Computer Science A—including FRQs, MCQs, debugging, and exam strategy—you can explore my private AP CSA online tutoring program.

Ideal for students targeting a score of 4 or 5 on the AP CSA exam.

WhatsApp: +91 9853166385
Email: javatution@gmail.com
Book a free demo: Contact Chinmay

Primary Sidebar

Mr Chinmay

Chinmay Patel
Book a Demo Class

Phone & Whatsapp +919853166385
javatution@gmail.com

Recent Posts

  • Learn Java in One Day: 10-Hour 1-on-1 Crash Course | JavaTutorOnline
  • Constructor in Java: Examples, Types & Constructor Overloading
  • Important Interview Questions on Java Multithreading
  • React Spring Boot Web Services React Integration
  • Spring Boot RESTful Web Services Example
  • Top Spring MVC Interview Questions and Answers for Developers
  • Top Spring Core Interview Questions and Answers for Developers
  • Host Java Web Apps for Free on Mobile with Tomcat and Termux
  • How to Deploy Java Web Application on Aws EC2 with Elastic IP
  • Simple Jsp Servlet Jdbc User Registration using Tomcat Mysql and Eclipse

Additional Resources

  • AP Computer Science A Summer Prep
Copyright © 2026 JavaTutorOnline