Question 371
What is printed?
String s = "ABCDEFGHIJ";
int m = s.length() / 2;
String left = s.substring(0, m);
String right = s.substring(m);
String result = right + left;
System.out.println(result.substring(3, 7));Main Solution
Step-by-step trace:
s = "ABCDEFGHIJ"(length = 10)m = 10 / 2 = 5left = s.substring(0, 5) = "ABCDE"right = s.substring(5) = "FGHIJ"result = right + left = "FGHIJABCDE"result.substring(3, 7)extracts characters at indices 3, 4, 5, 6 (inclusive start, exclusive end):
Index 0='F', 1='G', 2='H', 3='I', 4='J', 5='A', 6='B', 7='C'
So indices 3–6 = "IJAB"
The answer is D: IJAB.
Teacher Explanation
This question tests understanding of Java's String.substring() method (0-indexed, end-exclusive), integer division, and string concatenation. Students must correctly split the string at the midpoint, concatenate in the reversed order (right + left), and then apply a second substring on the resulting string.
Education Coach Advice
AP Exam Strategy: When tracing string manipulation code, write out each variable's value explicitly after each assignment. Pay special attention to the order of operands in concatenation (right + left vs. left + right) and remember that substring(a, b) includes index a but excludes index b. Number the characters of the final string starting from 0 to avoid off-by-one errors.
Traps
Common traps:
- Option A (GHIJ): Students may forget that
resultisright + left(notleft + right) and apply substring to the original string orleft + right. - Option B (FGHI): Students may apply
substring(3, 7)directly toright("FGHIJ"), getting indices 3–6 of "FGHIJ" = "HIJ" — or confuse the concatenation order. - Option C (ABIJ): Students may mix up which half is
leftvs.rightor miscount the midpoint.
Answer images

