Once you master basic loop syntax, the AP Computer Science A exam tests how you apply loops to solve standard algorithmic problems. Topic 4.3 focuses on common computational patterns including accumulator variables, string manipulation, character traversal, and digit extraction.
In this interactive practice set, we cover Topic 4.3: Developing Algorithms Using Loops. These 10 multiple-choice questions challenge your ability to trace state changes in strings and numbers during repetitive execution.
Key Concepts to Remember Before You Start:
- Accumulator Patterns: Initializing sums to
0or products to1before a loop, then updating them inside the loop body. - String Processing: Traversing strings using
substring(i, i + 1)combined with loop bounds likes.length(). - Reversing Strings: Building new strings backward by looping from
s.length() - 1down to0. - Digit Extraction: Isolating the rightmost digit of a number using modulo (
% 10) and stripping it using integer division (/ 10).
Test your knowledge with the interactive quiz below. Review the explanations for any questions you miss to sharpen your algorithm tracing skills!
AP CSA 4.3: Loop Algorithms
String rev = “”;
for (int i = str.length() – 1; i >= 0; i–) {
rev += str.substring(i, i + 1);
}
System.out.println(rev);
str.length() is 4, so i starts at index 3 (‘a’). The loop steps backward to 2 (‘v’), 1 (‘a’), and 0 (‘j’). Concatenating these characters builds the reversed string “avaj”.int count = 0;
for (int i = 0; i < s.length(); i++) {
if (s.substring(i, i + 1).equals(“o”)) {
count++;
}
}
System.out.println(count);
count increments to 2.int maxDigit = 0;
while (n > 0) {
int d = n % 10;
if (d > maxDigit) {
maxDigit = d;
}
n /= 10;
}
System.out.println(maxDigit);
n. Using modulo (% 10) and integer division (/ 10), it extracts digits from right to left: 5, then 2, then 8, and finally 4. The highest value encountered and stored in maxDigit is 8.String result = “”;
for (int i = 1; i < word.length(); i += 2) {
result += word.substring(i, i + 1);
}
System.out.println(result);
– Index 1: ‘o’
– Index 3: ‘p’
– Index 5: ‘t’
– Index 7: ‘r’
Combining these gives the string “optr”.
for (int i = 1; i <= 5; i++) {
sum += (i * 10);
}
System.out.println(sum);
i * 10 to sum. Iteration values added: 10 + 20 + 30 + 40 + 50 = 150.
int sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
System.out.println(sum);
sum accumulator. 4 + 3 + 2 + 1 = 10.String res = “”;
for (int i = 0; i < s.length(); i++) {
String c = s.substring(i, i + 1);
if (res.indexOf(c) < 0) {
res += c;
}
}
System.out.println(res);
indexOf(c) returns -1 (which is < 0) if a character is not yet in res. It appends ‘a’, ‘p’, ‘l’, and ‘e’. When it encounters the second ‘p’, indexOf("p") returns 1, so it is ignored. Output: “aple”.for (int i = 1; i <= 4; i++) {
val = val * 2 + i;
}
System.out.println(val);
– Start: val = 0
– i = 1: val = (0 * 2) + 1 = 1
– i = 2: val = (1 * 2) + 2 = 4
– i = 3: val = (4 * 2) + 3 = 11
– i = 4: val = (11 * 2) + 4 = 26
The loop terminates, printing 26.
int c = 0;
for (int i = 0; i < s.length() – 1; i++) {
if (s.substring(i, i + 2).equals(“ab”)) {
c++;
}
}
System.out.println(c);
i < s.length() - 1) safely allow substring(i, i + 2) to grab pairs of characters without crashing. It scans for the exact string “ab”, which appears twice in “abracadabra” (at the very beginning and near the end).n?%) returns the remainder of a division. Dividing any base-10 number by 10 leaves the rightmost digit as the remainder (e.g., 345 % 10 = 5). Using n / 10 chops off the rightmost digit instead of isolating it.Unit 4.3 Algorithms Practice Complete!
Why String Algorithms Dominate the FRQ Section
While you will see loop algorithms throughout the multiple-choice section, they are absolutely guaranteed to appear on the Free Response Questions (FRQs). The College Board frequently requires you to iterate through an entire string to find specific patterns, count occurrences of a target word, or build a newly modified string character-by-character.
Ready for the Next Topic?
Now that you know how to process single sequences and simple algorithms, it is time to look at what happens when loops are placed inside other loops.
Continue your AP CSA review by proceeding to Topic 4.4: Nested Iteration Practice, where we explore inner-to-outer execution limits and matrix logic.