AP CSA Unit-Level MCQ
String Traversal
Practice mode with 15 Java-focused questions, immediate answer checks, and explanations.
What string traversal covers
Traversing a String means looping an index from 0 while it is less than length() and examining each character with charAt. Because Strings are immutable, building a modified version means accumulating into a new String rather than editing in place. Common traversal jobs include counting occurrences, reversing text, filtering characters, and checking whether the text matches a pattern.
Where students lose points
Bounds errors dominate: using <= length() throws, and stopping at length() - 1 with a < test skips the final character. When comparing adjacent characters, students forget to shorten the loop bound by one and read past the end. Result accumulators also get initialized inside the loop, discarding everything built so far.
How it shows up on the AP exam
Unit 1 and Unit 2 combined, since traversal is where String methods meet iteration. This pairing is a standard FRQ setup and one of the most reliable long-tail MCQ topics.
For practice use only.
String Traversal MCQ Practice
AP CSA String traversal practice: looping over characters, building results with concatenation, counting matches, and staying inside valid index bounds.
Answered 0 of 15
Choose one answer.
Code Tracing 01 - String Traversal: track the final printed value.
String word = "ALGORITHM";
int count = 0;
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
if (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') count++;
}
System.out.print(count);All 15 string traversal questions
Work through the interactive quiz above first. This is the full string traversal question set with worked solutions, so students can review any question after attempting it.
1.Code Tracing 01 - String Traversal: track the final printed value.
String word = "ALGORITHM"; int count = 0; for (int i = 0; i < word.length(); i++) { char c = word.charAt(i); if (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') count++; } System.out.print(count);- 6
- 9
- 4
- 3
Show worked solution
Correct answer: 3
The loop inspects one character per pass and only advances the counter on vowels.
2.Code Tracing 12 - String Traversal: follow the variable updates.
String word = "PROGRAM"; String out = ""; for (int i = word.length() - 1; i >= 0; i--) { out += word.charAt(i); } System.out.print(out);- MARGORP
- PROGRAM
- ARGORP
- ROGRAM
Show worked solution
Correct answer: MARGORP
Starting at length() - 1 and counting down visits the characters in reverse order.
3.Code Tracing 28 - String Traversal: trace the branch and loop path.
String word = "METHODS"; String out = ""; for (int i = 1; i < word.length(); i += 2) { out += word.charAt(i); } System.out.print(out);- EHD
- MTOS
- METHODS
- HD
Show worked solution
Correct answer: EHD
Starting the index at 1 and stepping by 2 picks up the characters at odd positions only.
4.Code Tracing 39 - String Traversal: watch the index changes.
String word = "ARRAYS"; char target = word.charAt(0); int count = 0; for (int i = 0; i < word.length(); i++) { if (word.charAt(i) == target) count++; } System.out.print(target + ":" + count);- A:1
- A:2
- A:3
- 0:2
Show worked solution
Correct answer: A:2
Comparing char values with == is correct. The first character always matches itself, so the count is at least 1.
5.Code Tracing 50 - String Traversal: evaluate the state change step by step.
String word = "CODING"; String out = ""; for (int i = 0; i < word.length(); i++) { char c = word.charAt(i); if (c == 'A' || c == 'E') continue; out += c; } System.out.print(out);- The code does not compile.
- CODING
- CODINGAE
Show worked solution
Correct answer: CODING
continue skips the append for the matching characters, so they are filtered out of the result.
6.What is printed by the following code segment?
String s = "mississippi"; int c = 0; for (int i = 0; i < s.length() - 1; i++) { if (s.charAt(i) == s.charAt(i + 1)) { c++; } } System.out.print(c);- 3
- 4
- 2
- 11
Show worked solution
Correct answer: 3
The doubled pairs are ss, ss, and pp.
7.What is printed by the following code segment?
String s = "abcdef"; String out = ""; for (int i = 0; i < s.length(); i++) { out = i % 2 == 0 ? out + s.charAt(i) : s.charAt(i) + out; } System.out.print(out);- abcdef
- fdbace
- acebdf
- fdbeca
Show worked solution
Correct answer: fdbace
Even positions are appended and odd positions are prepended, so the two halves grow in opposite directions.
8.What is printed by the following code segment?
String s = "Hello World"; int u = 0; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c >= 'A' && c <= 'Z') { u++; } } System.out.print(u);- 10
- 11
- 2
- 1
Show worked solution
Correct answer: 2
Comparing a char against a range of letters works because char values are ordered, and two capitals appear.
9.What is printed by the following code segment?
String s = "abcba"; boolean p = true; int i = 0; int j = s.length() - 1; while (i < j) { if (s.charAt(i) != s.charAt(j)) { p = false; } i++; j--; } System.out.print(p);- false
- 5
- The code does not compile.
- true
Show worked solution
Correct answer: true
Two indexes converge from the ends and every mirrored pair matches.
10.What is printed by the following code segment?
String s = "a1b2c3"; int sum = 0; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c >= '0' && c <= '9') { sum += c - '0'; } } System.out.print(sum);- 6
- 150
- 3
- 0
Show worked solution
Correct answer: 6
Subtracting the character zero converts a digit character to its numeric value, and 1, 2, and 3 total 6.
11.What is printed by the following code segment?
String s = "aabbaa"; String out = ""; for (int i = 0; i < s.length(); i++) { if (i == 0 || s.charAt(i) != s.charAt(i - 1)) { out += s.charAt(i); } } System.out.print(out);- aabbaa
- aba
- ab
- abab
Show worked solution
Correct answer: aba
Only the first character of each run survives, which collapses consecutive duplicates.
12.What is printed by the following code segment?
String s = "abc"; String out = ""; for (int i = 0; i < s.length(); i++) { for (int j = i; j < s.length(); j++) { out += s.substring(i, j + 1) + " "; } } System.out.print(out.length());- 12
- 10
- 16
- 18
Show worked solution
Correct answer: 16
The six substrings have lengths 1, 2, 3, 1, 2, and 1, totalling 10, and each is followed by a space.
13.What is printed by the following code segment?
String s = "programming"; int c = 0; for (int i = 0; i < s.length(); i++) { if ("aeiou".indexOf(s.charAt(i)) >= 0) { c++; } } System.out.print(c);- 4
- 2
- 11
- 3
Show worked solution
Correct answer: 3
The vowels present are o, a, and i.
14.What is printed by the following code segment?
String s = "abcd"; String out = ""; for (int i = s.length(); i > 0; i--) { out += s.substring(i - 1, i); } System.out.print(out);- dcba
- abcd
- dcb
- abc
Show worked solution
Correct answer: dcba
The loop counts down and takes one character at a time from the end.
15.What is printed by the following code segment?
String s = "the quick fox"; int longest = 0; int run = 0; for (int i = 0; i < s.length(); i++) { run = s.charAt(i) == ' ' ? 0 : run + 1; if (run > longest) { longest = run; } } System.out.print(longest);- 3
- 5
- 13
- 4
Show worked solution
Correct answer: 5
The longest word is quick, with five characters.
