You have completed the core topics for Unit 4: Iteration. This comprehensive mock exam blends while loops, for loops, string algorithms, and nested loops into a single 15-question test designed to mimic the difficulty of the AP Computer Science A exam.
Exam Tips:
- Trace carefully: Do not guess. Write out a trace table for variables before selecting an answer.
- Check bounds: Pay close attention to
<vs<=, and whether strings process tolength()orlength() - 1. - Watch the scope: Remember that loop variables declared in a
forloop header do not exist outside the loop.
Unit 4 Iteration Mock Exam
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. It only exists within the loop’s scope. 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… meaning the method extracts and returns the characters located at the even indices of the string.for (int a = 0; a < 4; a++) {
for (int b = 1; b < 3; b++) {
System.out.println("Hi");
}
}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=2: j prints * for 2, 3 (**)
i=3: j prints * for 3 (*)
The pattern decreases by one star each row.
val contain after the loop executes?int val = 0;
for(int i = 2; i < 10; i *= 2) {
val += i;
}16 < 10 is false. 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);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 true for every pair EXCEPT (0,0), (1,1), and (2,2). So it executes 9 – 3 = 6 times.for (int i = 0; i < 10; i++) { /* body */ }for loop, the update expression (i++) executes *after* the body. Option B correctly replicates this logic by placing i++ at the very end of the while loop body.result?int result = 1;
for(int i = 4; i > 1; i--) {
result *= i;
}1 > 1 is false. Output is 24.int x = 10;
while (x ___ 0) {
x -= 2;
}!= 0 can be risky if x misses 0 (e.g. if x started odd). However, x > 0 guarantees the loop terminates safely once x becomes 0 or negative.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’. Next i=4, w.substring(3, 4) returns ‘l’. This is a standard reverse string loop, printing “olleh”.int c = 0;
for(int i = 0; i < 5; i++) {
for(int j = 0; j < i; j++) {
c++;
}
}
System.out.println(c);i=1: j<1 (1 time)
i=2: j<2 (2 times)
i=3: j<3 (3 times)
i=4: j<4 (4 times)
Total = 0 + 1 + 2 + 3 + 4 = 10.
Unit 4 Mock Exam Complete!
Congratulations on Completing Unit 4!
Iteration is arguably the most important foundational concept in all of computer science. By mastering while loops, for loops, and nested iteration, you now have the tools required to traverse data structures, process strings, and write complex algorithms.
Now that you know how to control the flow of your programs, it is time to learn how to design your own custom objects and behaviors. Continue your AP CSA journey by moving on to the next major module: Unit 5: Writing Classes.