AP Computer Science A, AP exams, 25–35%
Unit 2: Selection and Iteration
Unit 2 covers control flow, the largest single source of multiple-choice questions after data collections. Boolean expressions, conditionals, and loops are developed together with the reasoning needed to compare objects correctly and to evaluate equivalent Boolean expressions. Informal runtime analysis by counting statement executions is introduced here.
Unit 2: Selection and Iteration at a glance
- Position
- 2 of 4 units
- Published weighting
- 25–35% in the official framework
- Topics covered
- 6 topics in this guide
What is covered
Topics inside this unit.
These topics describe the scope students most often review for this part of AP Computer Science A. Treat them as a study checklist rather than an exhaustive list of what a single exam form can sample.
- Boolean expressions
- Relational and logical operators, short-circuit evaluation, and building compound conditions.
- Conditional statements
- The if, else if, and else structures, and nested conditionals with careful attention to which branch runs.
- Equivalent Boolean expressions
- Applying De Morgan's laws and simplifying conditions without changing behavior.
- Comparing objects
- Why the equals method and the double-equals operator differ for references, and comparing Strings correctly.
- While and for loops
- Loop control variables, termination conditions, and translating between the two loop forms.
- Nested iteration and runtime
- Loops inside loops, and counting how many times a statement executes as an informal efficiency measure.
Study tips
How to practice Selection and Iteration.
Section-specific guidance that follows from how this material is usually tested.
- Trace nested loops with a table of variable values on every iteration rather than reasoning about them abstractly.
- Practice comparing Strings with the equals method until using the double-equals operator on references feels wrong.
- Count statement executions for a nested loop as a routine exercise; it is a common multiple-choice item.
Where this fits
Unit 2: Selection and Iteration is 2 of 4 units in the AP Computer Science A guide, and it carries a published weighting of 25–35% in the official framework. Use the full guide to balance it against everything else you have to review.
Sample Selection and Iteration questions
Practice questions, not yet expert-reviewedOriginal practice questions written to the published framework for this section. No expert has reviewed them, and they are not taken from any official exam. Try each one before you open the answer.
- Question 1Foundational
How many times does the body of the Java loop for (int i = 0; i < 5; i++) execute?
- A5
- B4
- C6
- D0
Show answer
Answer: A. 5
The control variable takes the values 0, 1, 2, 3, and 4 before the condition i < 5 fails, so the body runs 5 times. Answering 4 counts the increments between those values rather than the values themselves.
Key insight: A loop from 0 while i < n runs exactly n times, even though it never reaches n.
- Question 2Foundational
Is the following statement true or false: in Java, using == on two String variables compares the sequence of characters they contain?
- ATrue
- BFalse
Show answer
Answer: B. False
For reference types, == asks whether two variables refer to the same object, not whether the objects hold equal contents. Comparing characters requires the equals method, and == can appear to work only because identical literals may share one interned object.
Key insight: Use equals to compare String contents; == compares references.
- Question 3Intermediate
What is the value of count after this Java code runs? int count = 0; for (int i = 0; i < 4; i++) { for (int j = i; j < 4; j++) { count++; } }
- A10
- B16
- C12
- D8
Show answer
Answer: A. 10
The inner loop runs 4 times when i is 0, then 3, then 2, then 1 time, because it starts at j = i rather than at 0. The total is 4 + 3 + 2 + 1 = 10; answering 16 assumes the inner loop always runs 4 times.
Key insight: When an inner loop starts at the outer index, the counts form a decreasing sum, not a product.
- Question 4Intermediate
Which Java expression is equivalent to !(x > 0 && y < 5)?
- Ax <= 0 || y >= 5
- Bx < 0 || y > 5
- Cx <= 0 && y >= 5
- Dx > 0 || y < 5
Show answer
Answer: A. x <= 0 || y >= 5
De Morgan's law turns the negation of an AND into an OR of the negated parts, and the negation of x > 0 is x <= 0 rather than x < 0. Keeping && instead of switching to || is the other classic error.
Key insight: Negating a compound condition flips both the comparisons and the connective.
- Question 5Advanced
What is the value of count after this Java code runs? int n = 27; int count = 0; while (n > 1) { n = n / 2; count++; }
- A4
- B5
- C3
- D27
Show answer
Answer: A. 4
Integer division makes n take the values 13, 6, 3, and 1, so the body runs 4 times before the condition fails. Expecting 5 counts an extra halving by treating 27 / 2 as 13.5 instead of 13.
Key insight: Repeated integer halving of n takes about log base 2 of n steps, with truncation at every step.
Continue through AP Computer Science A
Practice Selection and Iteration deliberately.
Create your Arcade Academy workspace to turn this topic list into spaced-repetition reviews, linked notes, and progress you can act on.