• 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
  • Courses
  • Tutorials
    • Java
    • Servlets
    • Struts
    • Spring
    • Webservice
  • FAQ
  • Testimonials
  • Blog
  • CONTACT US

Strings – FRQ Practice (AP Computer Science A)

Strings FRQ Practice Ap Computer Science

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 countVowels(String s) that returns how many vowels (a, e, i, o, u) appear in the string. Ignore case.

Example:

“Computer” → 3

Approach
  • Convert the string to lowercase
  • Traverse each character
  • Check membership in "aeiou"
Java Solution

public int countVowels(String s) {
    int count = 0;
    String low = s.toLowerCase();

    for (int i = 0; i < low.length(); i++) {
        if ("aeiou".indexOf(low.charAt(i)) != -1) {
            count++;
        }
    }
    return count;
}
    
Common Mistakes
  • Ignoring uppercase vowels
  • Checking only one vowel
  • Using == for String comparison

FRQ 2 — Remove All Digits

Easy
Problem:

Write removeDigits(String s) that returns a new string with all digits removed.

Example:

"a1b2c3d4" → "abcd"

Approach
  • Traverse the string
  • Append only non-digit characters
  • Use Character.isDigit()
Java Solution

public String removeDigits(String s) {
    StringBuilder result = new StringBuilder();

    for (int i = 0; i < s.length(); i++) {
        if (!Character.isDigit(s.charAt(i))) {
            result.append(s.charAt(i));
        }
    }
    return result.toString();
}
    
Common Mistakes
  • Using + inside a loop
  • Returning null instead of empty string
  • Incorrect digit detection

FRQ 3 — Reverse a String

Easy
Problem:

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

Example:

"java" → "avaj"

Approach
  • Traverse from the last index to 0
  • Append characters to a new string
Java Solution

public String reverseString(String s) {
    StringBuilder rev = new StringBuilder();

    for (int i = s.length() - 1; i >= 0; i--) {
        rev.append(s.charAt(i));
    }
    return rev.toString();
}
    
Common Mistakes
  • Off-by-one loop errors
  • Traversing forward instead of backward

FRQ 4 — Count Words

Easy
Problem:

Assume words are separated by a single space. Return the number of words in the string.

Example:

"AP Computer Science" → 3

Approach
  • Count spaces
  • Words = spaces + 1
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.charAt(i) == ' ') {
            count++;
        }
    }
    return count;
}
    
Common Mistakes
  • Not handling empty string
  • Counting multiple spaces as multiple words

FRQ 5 — Find the Longest Word

Medium
Problem:

Write longestWord(String sentence) that returns the longest word. Assume words are separated by spaces.

Example:

"I love programming in Java" → "programming"

Approach
  • Split the sentence using spaces
  • Track the longest word
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
  • Using >= instead of >
  • Not handling empty sentence

FRQ 6 — Remove Vowels

Medium
Problem:

Write removeVowels(String s) that removes all vowels (case-insensitive) from the string.

Example:

"Computer" → "Cmptr"

Approach
  • Traverse each character
  • Convert to lowercase for checking
  • Append only non-vowel characters
Java Solution

public String removeVowels(String s) {
    StringBuilder result = new StringBuilder();

    for (int i = 0; i < s.length(); i++) {
        char c = Character.toLowerCase(s.charAt(i));
        if ("aeiou".indexOf(c) == -1) {
            result.append(s.charAt(i));
        }
    }
    return result.toString();
}
    
Common Mistakes
  • Forgetting uppercase vowels
  • Removing original character case

FRQ 7 — Check Palindrome

Medium
Problem:

Write isPalindrome(String s) that returns true if the string reads the same forwards and backwards.

Example:

"level" → true

Approach
  • Use two pointers from start and end
  • Compare characters
  • Stop early if mismatch occurs
Java Solution

public boolean isPalindrome(String s) {
    int left = 0;
    int right = s.length() - 1;

    while (left < right) {
        if (s.charAt(left) != s.charAt(right)) {
            return false;
        }
        left++;
        right--;
    }
    return true;
}
    
Common Mistakes
  • Incorrect pointer movement
  • Comparing wrong indices

FRQ 8 — Count Uppercase Letters

Medium
Problem:

Write countUppercase(String s) that returns the number of uppercase letters in the string.

Example:

"ApCSa" → 2

Approach
  • Traverse the string
  • Use Character.isUpperCase()
Java Solution

public int countUppercase(String s) {
    int count = 0;

    for (int i = 0; i < s.length(); i++) {
        if (Character.isUpperCase(s.charAt(i))) {
            count++;
        }
    }
    return count;
}
    
Common Mistakes
  • Manual ASCII comparisons
  • Counting lowercase letters

FRQ 9 — Count Substring Occurrences

Hard
Problem:

Write countOccurrences(String s, String sub) that counts overlapping occurrences of sub.

Example:

"aaaa", "aa" → 3

Approach
  • Slide a window of length sub.length()
  • Move one index at a time
  • Compare substrings
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 == instead of equals()
  • Loop boundary errors
  • Missing overlapping matches

FRQ 10 — Validate ID Format

Hard
Problem:

A valid ID has 2 letters followed by 4 digits. Return true if the format is valid.

Example:

"AB1234" → true

Approach
  • Check string length
  • Validate letters and digits separately
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 string length first
  • Accessing invalid indices

FRQ 11 — Remove Extra Spaces

Hard
Problem:

Replace multiple consecutive spaces with a single space.

Example:

"AP Computer Science" → "AP Computer Science"

Approach
  • Traverse characters one by one
  • Append character if previous is not a space
Java Solution

public String removeExtraSpaces(String s) {
    StringBuilder result = new StringBuilder();

    for (int i = 0; i < s.length(); i++) {
        if (i == 0 || s.charAt(i) != ' ' || s.charAt(i - 1) != ' ') {
            result.append(s.charAt(i));
        }
    }
    return result.toString();
}
    
Common Mistakes
  • Using split() unnecessarily
  • Not handling leading spaces

FRQ 12 — Extract Digits

Hard
Problem:

Return a string containing only digits from the input string.

Example:

"ab12cd34" → "1234"

Approach
  • Traverse each character
  • Append digits only
Java Solution

public String extractDigits(String s) {
    StringBuilder result = new StringBuilder();

    for (int i = 0; i < s.length(); i++) {
        if (Character.isDigit(s.charAt(i))) {
            result.append(s.charAt(i));
        }
    }
    return result.toString();
}
    
Common Mistakes
  • Returning an integer instead of a string
  • Manual digit comparison

FRQ 13 — Longest Consecutive Character

Hard
Problem:

Return the character with the longest consecutive repetition. Assume the string is non-empty.

Example:

"aaabbccccd" → 'c'

Approach
  • Track current streak length
  • Track longest streak and character
Java Solution

public char longestStreak(String s) {
    char best = s.charAt(0);
    int bestCount = 1;
    int count = 1;

    for (int i = 1; i < s.length(); i++) {
        if (s.charAt(i) == s.charAt(i - 1)) {
            count++;
            if (count > bestCount) {
                bestCount = count;
                best = s.charAt(i);
            }
        } else {
            count = 1;
        }
    }
    return best;
}
    
Common Mistakes
  • Not resetting count
  • Not updating best character

FRQ 14 — Compress String

Hard
Problem:

Compress consecutive characters using counts.

Example:

"aaabbc" → "a3b2c1"

Approach
  • Count consecutive characters
  • Append character followed by count
Java Solution

public String compress(String s) {
    StringBuilder result = new StringBuilder();
    int count = 1;

    for (int i = 1; i <= s.length(); i++) {
        if (i < s.length() && s.charAt(i) == s.charAt(i - 1)) {
            count++;
        } else {
            result.append(s.charAt(i - 1)).append(count);
            count = 1;
        }
    }
    return result.toString();
}
    
Common Mistakes
  • Missing the last character group
  • Loop boundary errors

FRQ 15 — Mask Email Username

Hard
Problem:

Mask all characters of the email username except the first and last character.

Example:

"john.doe@gmail.com" → "j*****e@gmail.com"

Approach
  • Separate username and domain
  • Replace middle characters with '*'
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;

    StringBuilder masked = new StringBuilder();
    masked.append(user.charAt(0));

    for (int i = 1; i < user.length() - 1; i++) {
        masked.append('*');
    }

    masked.append(user.charAt(user.length() - 1));
    return masked.toString() + domain;
}
    
Common Mistakes
  • Masking the domain part
  • Not handling short usernames

FRQ Strategy Tips for Strings (AP CSA)

  • Always clarify assumptions before coding
  • Prefer index-based loops over enhanced for-loops
  • Use StringBuilder when building new strings
  • Watch loop boundaries carefully
  • Partial credit is awarded for correct logic even if minor bugs exist

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 dedicated
tutoring program here:

AP Computer Science A tutoring support
https://www.javatutoronline.com/training-courses/ap-computer-science-tutor/

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

Primary Sidebar

Mr Chinmay

Chinmay Patel
Online Java Tutor-Demo Class

Phone & Whatsapp +919853166385
javatution@gmail.com

Recent Posts

  • How to Learn Java in One day? | Crash Course | JavaTutorOnline
  • Constructor in Java and Overloaded Constructor Example Program
  • Important Interview Questions on Java Multithreading
  • React Spring Boot Web Services 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
Copyright © 2026 JavaTutorOnline