AP CSA 2.10 FRQ Practice: Scanning a String

Unit 2: Selection and Iteration · Lesson 2.10 · FRQ Practice

Scanning a String

A free response question in the shape the exam uses: a stated contract, four rubric parts, and no main method handed to you. Worth 4 points.

Why this question is worth four points

Every String algorithm on the exam is a loop from 0 to length() - 1 with charAt inside it. The two ways to lose the point are looping to length() (which throws) and comparing characters with equals (which does not compile for a char).

What you are given

Nothing is given. Write a class named Scan containing exactly the four static methods described below. Assume every String argument is not null. Do not write a main method.

The question

  1. (a) public static int countChar(String s, String target) returns how many times the first character of target appears in s. Assume target has at least one character.
  2. (b) public static String reverse(String s) returns s with its characters in the opposite order.
  3. (c) public static boolean isPalindrome(String s) returns whether s reads the same forwards and backwards. The empty String is a palindrome.
  4. (d) public static int countVowels(String s) returns how many of the characters a, e, i, o and u appear in s, lower case only.

What the reader is looking for

  1. Write class Scan with the four static methods exactly as described. No main method.
  2. A loop over a String runs from 0 while i < s.length(). Using <= reads one character past the end and throws.
  3. Characters compare with ==, not equals. s.charAt(i) == 'a' is the comparison; s.charAt(i).equals("a") does not compile.

Worked examples

These show what a correct answer prints. There are more cases you cannot see, and they use different values, so an answer built around just these numbers will fail.

Example 1 input
banana a
Example 1 output
3
ananab
false
3
Example 2 input
racecar r
Example 2 output
2
racecar
true
3

Your answer

Main.java

Input for the Run button

How this is scored: your answer runs against every test case, and the fraction it passes becomes your score out of 4. That is not how a human AP reader marks a rubric, so treat the score as a check on whether your code works, and the rubric above as the thing you are actually practising.


  

  

Stuck?

Hint 1

The last valid index is length() - 1. A loop that runs while i <= s.length() throws StringIndexOutOfBoundsException on the very last pass.

Hint 2

Building a String by adding characters one at a time is fine here. Start with the empty String, which is the identity for concatenation exactly as 0 is for a sum.

Hint 3

Part (c) is one line once part (b) works. A palindrome is a String equal to its own reversal, and reusing your own method is both shorter and harder to get wrong.

Before you submit

4 mistake(s) that lose points on this question

Each of these is a real error the grader catches. Check your answer against them before you submit, not instead of trying.

  • part (b) builds the reversal forwards, returning the original
  • part (a) counts every character rather than only the target
  • part (d) misses the vowel u
  • part (c) always returns true

Where to go next

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]