• 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 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/

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 © 2025 JavaTutorOnline