Unit 2 Practice Test: Selection and Iteration
Sharpen AP Computer Science A control flow skills with Unit 2 practice tests covering if/else, while loops, for loops, nested loops, and boolean logic in Java.
Selection and Iteration in AP Computer Science A
Unit 2 introduces control flow — the mechanisms that allow a Java program to make decisions and repeat actions. This unit is among the most heavily tested on the AP exam, appearing in both MCQ code-tracing questions and FRQ code-writing tasks.
Conditional Statements: if, else if, else
Java's if/else if/else structure allows programs to execute different code paths based on conditions. AP MCQ questions often present multi-branch conditionals and ask you to trace which branch executes for a given input. Carefully evaluating boolean expressions at each branch is critical.
Boolean Expressions and Compound Conditions
Boolean logic underpins all control flow. The AP exam tests && (and), || (or), and ! (not) operators, as well as De Morgan's Laws for simplifying or negating compound conditions. Misreading a compound condition is one of the most common sources of MCQ errors.
while Loops
A while loop repeats as long as its condition remains true. AP tracing questions require you to track variable values across each iteration. A frequent FRQ task involves writing a while loop that processes input until a sentinel value is reached.
for Loops
The for loop provides a compact syntax for iteration with an initializer, condition, and update expression. AP CSA tests standard for loops traversing integer ranges and enhanced for loops traversing arrays and ArrayLists in Unit 4.
Nested Loops
Nested loops appear frequently in AP FRQs involving 2D arrays and pattern generation. Tracing nested loops requires careful attention to which variable belongs to the outer loop and which belongs to the inner loop at each step.
Common Errors in Unit 2
- Off-by-one errors: Loop boundaries that are one too many or too few — a classic AP trap in both MCQ and FRQ.
- Infinite loops: A loop whose condition never becomes false because the update variable is never modified correctly.
- Incorrect boolean logic: Confusing && with ||, or failing to apply De Morgan's Laws when negating conditions.
- Incorrect if/else chaining: Misplacing an else so it attaches to the wrong if statement.
FRQ Skills Built in Unit 2
Writing loops and conditionals from scratch is a core FRQ requirement. Unit 2 practice builds the habit of planning loop logic before writing code — identifying the starting value, the termination condition, and the update step before attempting to implement the loop body.