Unit 2 — Selection and Iteration
Unit 2 is about controlling program flow: making decisions (booleans, if) and repeating steps (loops).
Study the topics first, then take the Unit 2 quiz: 15 MCQ + 1 FRQ.
Topic pages
Go in order (2.1 → 2.12).
Algorithms with Selection and Repetition2.1
Boolean Expressions2.2
if Statements2.3
Nested if Statements2.4
Compound Boolean Expressions2.5
Comparing Boolean Expressions2.6
while Loops2.7
for Loops2.8
Implementing Selection and Iteration Algorithms2.9
Implementing String Algorithms2.10
Nested Iteration2.11
Informal Run-Time Analysis2.12
How to use the quiz
- Answer MCQs first. Don’t check as you go.
- Click Check Answers to see score + explanations.
- Try the FRQ, then reveal the sample solution.
Unit 2 Quiz — Multiple Choice (15)
Choose the best answer. (One correct option each.)
1) Which statement best describes a loop?
Answer: B. Loops repeat statements while a condition holds (or for a set number of iterations).
2) Which expression evaluates to true when x is 7?
int x = 7;
Answer: D.
x >= 7 is true when x is 7.3) What is the value of y after the code executes?
int x = 5;
int y = 0;
if (x > 3) {
y = 10;
}
Answer: A. Since
x > 3 is true, the assignment in the if-block runs.4) Consider the code segment. What is printed?
int a = 4;
if (a > 2) {
if (a < 4) {
System.out.print("X");
} else {
System.out.print("Y");
}
} else {
System.out.print("Z");
}
Answer: C.
a>2 is true, then a<4 is false when a=4, so it prints Y.5) Which expression is equivalent to “x is between 1 and 10 inclusive”?
Answer: B. “Between inclusive” requires both conditions to be true, so use
&&.6) Assume p and q are boolean expressions. Which is always equivalent to !(p && q)?
Answer: D. De Morgan’s Law:
!(p && q) ⇔ !p || !q.7) How many times does the loop body execute?
int i = 0;
while (i < 3) {
i++;
}
Answer: A.
i becomes 1,2,3; body runs for i=0,1,2 → 3 times.8) What is the final value of sum?
int sum = 0;
for (int k = 1; k <= 4; k++) {
sum += k;
}
Answer: C. Adds 1+2+3+4 = 10.
9) Which is a correct reason to use a while loop instead of a for loop?
Answer: B.
while is common when repetition depends on a condition that may become true at unpredictable times.10) What is printed?
int n = 5;
if (n % 2 == 0) {
System.out.print("E");
} else {
System.out.print("O");
}
Answer: D.
5 % 2 is 1, so the else branch runs.11) Consider the code segment. What is the value of count after it executes?
int count = 0;
for (int i = 0; i < 5; i++) {
if (i % 2 == 0) {
count++;
}
}
Answer: A. Even i values in 0..4 are 0,2,4 → 3 times.
12) Which code segment correctly counts how many times the letter 'a' appears in s?
String s = "banana";
Answer: C. Loops through all indices 0..length-1 and compares each 1-char substring to "a".
13) What is the output of the code segment?
int total = 0;
for (int r = 0; r < 2; r++) {
for (int c = 0; c < 3; c++) {
total++;
}
}
System.out.print(total);
Answer: B. Inner loop runs 3 times for each of 2 outer iterations: 2*3 = 6.
14) Which describes informal run-time analysis in AP CSA terms?
Answer: D. Informal analysis focuses on growth trends (e.g., linear vs. quadratic) rather than exact timing.
15) What is the value of x after the code executes?
int x = 10;
while (x > 0) {
x -= 4;
}
Answer: A. Sequence: 10→6→2→-2, then stops because condition
x>0 is false.Unit 2 Quiz — FRQ (1)
This FRQ focuses on selection + iteration (Unit 2 skills).
FRQ 1) countMultiples
Write a static method countMultiples that counts how many integers in the range
1 through limit (inclusive) are multiples of k.
- Method header:
public static int countMultiples(int limit, int k) - Return the count of integers
nsuch that1 <= n <= limitandn % k == 0. - Assume
limit >= 1andk >= 1. - Example:
countMultiples(10, 3)returns3(3, 6, 9).
Write your solution here:
Sample solution
public static int countMultiples(int limit, int k) {
int count = 0;
for (int n = 1; n <= limit; n++) {
if (n % k == 0) {
count++;
}
}
return count;
}
Uses a loop to examine each number and an if statement to count multiples.