AP® Computer Science A Selection and Iteration Multiple Choice Questions
Work through AP Computer Science A multiple-choice questions on Selection and Iteration, then check each answer against a worked solution.
Free · no sign-up Open questions need no account. Sign up or log in to open the rest.
Subtopic: Iteration Clear filter
Question 202
Question 203
Question 204
Question 205
Question 206
Question 207
int n = 128;
int steps = 0;
while (n > 1)
{
n = n / 2;
steps = steps + 1;
}
System.out.println(steps);
What is printed when the code segment executes?Main Solution
Answer
Answer: C. The loop halves n (using integer division) on every iteration and increments steps. Starting from 128: 128 → 64 → 32 → 16 → 8 → 4 → 2 → 1. That is 7 halvings, so steps ends at 7. The loop then exits because n > 1 is false when n == 1. Option B is obtained by stopping one step early; option D adds an extra division that would take n to 0, but the guard prevents it.
Answer: Correct: C
A
5
B
6
C
7
D
8
Answer images
Question 208
Question 209
AP® and Advanced Placement® are registered trademarks of the College Board, which is not affiliated with, and does not endorse, Verityprep. Names are used for identification purposes only.

