This 15-question AP CSA practice assessment covers the currently available iteration topics in this collection: Topics 2.7–2.11.
The questions include while loops, for loops,
loop tracing, string algorithms, nested iteration, and common loop-control
patterns.
Important: This is a focused practice assessment for the currently available Topics 2.7–2.11. It is not a comprehensive assessment of every Unit 2 topic.
Practice Tips:
- Trace carefully: Write down the values of important variables rather than guessing.
-
Check loop bounds:
Pay close attention to
<versus<=, and determine exactly when a loop stops. -
Watch scope:
A variable declared in a
forloop header has scope limited to that loop. - Trace nested loops systematically: Complete the inner loop for each iteration of the outer loop.
Unit 2 Iteration & Algorithms Practice Assessment
int k = 10;
while (k > 0) {
System.out.print(k + " ");
k -= 3;
}k starts at 10 and decreases by 3 each iteration:
10, 7, 4, 1.When k becomes -2, the condition
-2 > 0 is false, so the loop ends.int sum = 0;
for (int i = 0; i < 5; i++) {
sum += i;
}
System.out.println(i);i is declared in the
for loop header. Its scope is limited to the loop.
Attempting to print it outside the loop causes a compilation error.public String process(String str) {
String res = "";
for (int i = 0; i < str.length(); i++) {
if (i % 2 == 0) {
res += str.substring(i, i + 1);
}
}
return res;
}i % 2 == 0 is true for indices
0, 2, 4, 6, and so on. The method therefore extracts the
characters located at the even indices."Hi" be printed?for (int a = 0; a < 4; a++) {
for (int b = 1; b < 3; b++) {
System.out.println("Hi");
}
}a = 0, 1, 2, 3.The inner loop runs 2 times:
b = 1, 2.Therefore:4 × 2 = 8.
result after the code executes?int result = 0;
int x = 5;
while (x > 5) {
result += x;
x--;
}5 > 5 is false.
The loop body executes zero times, leaving result
at its initial value of 0.num?% 10, adds it to the sum, and then removes it from
the number using integer division / 10.for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (j >= i) {
System.out.print("*");
}
}
System.out.println();
}i = 1: the condition is true for
j = 1, 2, 3 → ***i = 2: the condition is true for
j = 2, 3 → **i = 3: the condition is true for
j = 3 → *The pattern therefore decreases by one star on each row.
val contain after the loop executes?int val = 0;
for (int i = 2; i < 10; i *= 2) {
val += i;
}i starts at 2 and doubles after each iteration:i = 2, i = 4, i = 8.When
i becomes 16,
16 < 10 is false.Therefore:
val = 2 + 4 + 8 = 14.String s = "Mississippi";
int c = 0;
for (int i = 0; i < s.length() - 1; i++) {
if (s.substring(i, i + 2).equals("ss")) {
c++;
}
}
System.out.print(c);"Mississippi", the substring
"ss" occurs twice.Therefore, c becomes 2.count++ execute?int count = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i != j) {
count++;
}
}
}i != j is false for exactly three pairs:(0,0),
(1,1),
(2,2).Therefore:9 – 3 = 6.
for (int i = 0; i < 10; i++) {
/* body */
}for loop, the update expression
i++ executes after the loop body.Option B preserves that order:1. Test
i < 102. Execute the body
3. Execute
i++4. Repeat
result?int result = 1;
for (int i = 4; i > 1; i--) {
result *= i;
}1 × 4 = 4
4 × 3 = 12
12 × 2 = 24
When
i becomes 1, the condition
1 > 1 is false.Therefore, result is 24.int x = 10;
while (x ___ 0) {
x -= 3;
}x > 0, the loop continues while
x is positive.Starting from 10, the values are:10 → 7 → 4 → 1 → -2
Once
x becomes -2, the condition
-2 > 0 is false, so the loop terminates.By contrast,
x != 0 would not be safe here because
the sequence skips over 0.String w = "hello";
for (int i = w.length(); i > 0; i--) {
System.out.print(w.substring(i - 1, i));
}i starts at 5.w.substring(4, 5) returns
"o".Then
i = 4 and
w.substring(3, 4) returns
"l".Continuing in this way produces:olleh.
int c = 0;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < i; j++) {
c++;
}
}
System.out.println(c);i = 0: inner loop executes 0 times.i = 1: inner loop executes 1 time.i = 2: inner loop executes 2 times.i = 3: inner loop executes 3 times.i = 4: inner loop executes 4 times.Total:0 + 1 + 2 + 3 + 4 = 10.
Unit 2 Iteration & Algorithms Practice Complete!
What This Unit 2 Practice Assessment Covers
This assessment focuses on the currently available iteration and algorithm practice in this collection:
- Topic 2.7: while Loops
- Topic 2.8: for Loops
- Topic 2.9: Implementing Selection and Iteration Algorithms
- Topic 2.10: Implementing String Algorithms
- Topic 2.11: Nested Iteration
These questions are designed to give you additional practice tracing Java code and reasoning about loop behavior. For the complete current Unit 2 framework, use the official Unit 2 practice hub.
Continue Your AP CSA Practice
Return to the AP CSA Unit 2: Selection and Iteration MCQ Practice hub to review the complete Unit 2 topic structure and access the currently available practice pages.