AP CSA String Traversal
String Traversal in AP CSA: Complete Guide (2025-2026)
String traversal in AP CSA means iterating over a String character by character using a loop, and it is tested in both Unit 2 MCQ questions and FRQ solutions on the AP exam. Java Strings are zero-indexed: the first character is at index 0, the last is at index length() - 1. The key methods you must know are charAt(i), substring(a,b), length(), and indexOf(str). Every one of these methods appears regularly on the AP CSA exam.
📄 Table of Contents
💻 Code Examples — Predict First
Before running each example, write down your prediction. This is the single most effective AP exam study technique.
🤔 Predict the output before running:
public class Main {
public static void main(String[] args) {
String word = "hello";
for (int i = 0; i < word.length(); i++) {
System.out.print(word.charAt(i) + " ");
}
}
}
h e l l o — Loop runs i=0 to i=4 (length=5, condition i<5). charAt(i) returns the character at each index.
🤔 Predict the output before running:
public class Main {
public static void main(String[] args) {
String s = "banana";
int vowels = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c=='a' || c=='e' || c=='i' || c=='o' || c=='u') {
vowels++;
}
}
System.out.println(vowels);
}
}
3 — 'banana' has vowels at indices 1(a), 3(a), 5(a). Counter increments 3 times.
🤔 Predict the output before running:
public class Main {
public static void main(String[] args) {
String s = "abcde";
String result = "";
for (int i = s.length() - 1; i >= 0; i--) {
result += s.charAt(i);
}
System.out.println(result);
}
}
edcba — Loop starts at last index (length-1=4) and counts down to 0. Each character is appended to build the reversed string.
❌ Common Pitfalls
These are the mistakes students most often make on the AP CSA exam with String traversal AP CSA. Study them carefully.
The last valid index is length()-1. Using i <= word.length() causes a StringIndexOutOfBoundsException on the extra iteration. Always use i < word.length().
for (int i=0; i<=word.length(); i++) // THROWS EXCEPTION
word.charAt(i); // crashes when i == length
substring(a,b) returns characters from index a up to but NOT including index b. So "hello".substring(1,3) returns "el", not "ell". The AP exam tests this boundary every year.
"abcde".substring(1, 4) // returns "bcd" (NOT "bcde")
charAt(i) returns a char primitive, so == is correct for single-character comparison. But comparing two String objects requires .equals(). Mixing these up is a frequent AP trap.
When traversing backwards, the starting index must be length()-1, NOT length(). Starting at length() causes an immediate exception on the first call to charAt().
for (int i=s.length(); i>=0; i--) // CRASHES on first iteration
s.charAt(i); // index length is out of bounds
AP CSA FRQs frequently ask you to build a new String by traversing an existing one. Always initialize the result String to an empty string ("") before the loop, then use string concatenation inside. Returning before the loop or forgetting the initialization are the two most common FRQ errors.
substring(a,b) has length b − a. So s.substring(2,5) returns 3 characters. This formula is faster than counting on the AP exam.
✍ Check for Understanding (8 Questions)
"computer".charAt(3) return?
"abcdef".substring(2,5) return?
String s = "hello";
for (int i=0; i
"racecar".indexOf("car") return?
for (int i=0; i<=s.length(); i++)
System.out.println(s.charAt(i));
String r="";
for(int i=0;i if(i%2==0) r+=s.charAt(i);
System.out.println(r);
❓ Frequently Asked Questions
Use a for loop: 'for (int i = 0; i < s.length(); i++)'. Access each character with s.charAt(i). The index runs from 0 to length()-1 inclusive. Using <= instead of < causes a StringIndexOutOfBoundsException.
charAt(i) returns the char at index i in the String. Strings are zero-indexed, so the first character is at index 0 and the last is at index length()-1.
substring(a, b) returns a new String containing characters from index a up to but NOT including index b. The length of the result is b - a.
Compare the char returned by charAt() to each vowel using ==: if (c=='a' || c=='e' || c=='i' || c=='o' || c=='u'). Since charAt() returns a char primitive, == is the correct comparison (not .equals()).
Initialize an empty String result = "". Use a for loop starting at index length()-1 and counting down to 0. In each iteration, append the current character: result += s.charAt(i). After the loop, result is the reversed string.
Tanner Crow — AP CS Teacher & Tutor
11+ years teaching AP Computer Science at Blue Valley North High School (Overland Park, KS). Verified Wyzant tutor with 1,845+ hours, 451+ five-star reviews, and a 5.0 rating. His AP CSA students score 5s at more than double the national rate.
- 54.5% of students score 5 on AP CSA (national avg: 25.5%)
- 1,845+ verified tutoring hours • 5.0 rating
- Free 1-on-1 tutoring inquiry: Wyzant Profile
🔗 Related Topics
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.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]