DAY 3 | Selection + Iteration
Unit 2 | 25–35% of exam | Most MCQ trace questions come from this unit
Your Plan for Today
Read the summary below.
apcsexamprep.com/pages/unit-2-selection-and-iteration-ultimate-study-guide
Focus: boolean expressions, if/else if/else chains, for/while loop tracing.
Use the Unit 2 practice exam.
apcsexamprep.com/pages/ap-csa-unit-2-practice-exam
Do 10 questions. Track which type trips you up most.
-> Score 8-10: move to Step 3.
-> Score 5-7: reread the loop tracing section, then do Step 3.
-> Score 0-4: reread Unit 2 study guide. Try the loop tracing game.
apcsexamprep.com/pages/ap-csa-loop-tracing-game
| Your Score |
What to Do |
| 0–3 pts |
Reread loop section in Unit 2 study guide and try 2024 FRQ 2 as well. |
| 4–6 pts |
Review the rubric for what you missed, then move to Day 4. |
| 7–9 pts |
Strong. Move to Day 4. |
Concept Review
Boolean Expressions
-
&& (AND): both must be true | || (OR): at least one must be true
-
! (NOT): flips true to false and false to true
- Short-circuit:
false && anything stops early — true || anything stops early
-
== compares VALUES for primitives | .equals() compares CONTENT for objects
- Common bug: using
= (assignment) instead of == (comparison) inside a condition
Loop Patterns
-
for loop: use when you know how many times to repeat
-
while loop: use when you repeat until a condition is false
-
enhanced for: use to READ every element — cannot modify by index
- Off-by-one:
i < n runs n times | i <= n runs n+1 times
- Infinite loop: condition NEVER becomes false — common exam trap
// Trace this before reading choices
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= i * 2 - 1; j++)
{
System.out.print("* ");
}
System.out.println();
}
// i=1: j runs 1 time -> *
// i=2: j runs 3 times -> * * *
// i=3: j runs 5 times -> * * * * *
Practice Questions
Q11. [TRACE]
What is the output of the code below?
for (int k = 0; k <= 6; k += 2)
{
System.out.print(k + " ");
}
- (A) 0 2 4
- (B) 0 2 4 6
- (C) 1 3 5
- (D) 0 1 2 3 4 5 6
▶ REVEAL ANSWER
Answer: B
k starts at 0. Condition is k <= 6 (not k < 6). k increments by 2: 0, 2, 4, 6. When k=8, 8 <= 6 is false. Output: 0 2 4 6.
Q12. [SPOT THE ERROR]
This method should return true if val is between low and high, inclusive. What is the bug?
public static boolean inRange(int val, int low, int high)
{
if (val >= low && val <= high)
{
return false; // BUG: should be true
}
return true; // BUG: should be false
}
- (A) Should use || instead of &&
- (B) Boundary conditions should use > and < instead of >= and <=
- (C) The return values are swapped -- true and false are reversed
- (D) There is no bug
▶ REVEAL ANSWER
Answer: C
The condition correctly identifies values in range, but when it's TRUE the method returns false, and when FALSE it returns true. Fix: return (val >= low && val <= high); directly.
Q13. [TRACE]
How many times does System.out.println execute?
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= i * 2 - 1; j++)
{
System.out.println("AP CSA");
}
}
- (A) 6
- (B) 9
- (C) 12
- (D) 15
▶ REVEAL ANSWER
Answer: B
i=1: j runs 1 time. i=2: j runs 3 times. i=3: j runs 5 times. Total: 1+3+5 = 9.
Q14. [I/II/III]
Which of the following statements about loops in Java are TRUE?
I. Any for loop can be rewritten as an equivalent while loop.
II. An enhanced for loop allows you to modify elements of an array during traversal.
III. A while(true) loop will always run forever.
- (A) I only
- (B) I and II only
- (C) I and III only
- (D) I, II, and III
▶ REVEAL ANSWER
Answer: A
I is TRUE. II is FALSE -- the loop variable is a copy; reassigning it does not change the array. III is FALSE -- while(true) can exit via break or return.
Q15. [TRACE]
What is the value of result after this code executes?
int n = 5;
int result = 0;
for (int i = 1; i <= n; i++)
{
result += i;
}
- (A) 10
- (B) 15
- (C) 20
- (D) 25
▶ REVEAL ANSWER
Answer: B
i goes from 1 to 5. result = 0+1+2+3+4+5 = 15.
Answer Key (for printing)
Q11: B Q12: C Q13: B Q14: A Q15: B