AP® Computer Science A Selection and Iteration Practice Questions
Work through AP Computer Science A practice 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.
Question 422
Question 423
Question 424
a, b, and c are boolean variables.
!(a && (b || !c))
Which of the following is logically equivalent to the expression above?Main Solution
Answer
Answer: C. Apply De Morgan's law twice. !(a && X) becomes !a || !X, where X = (b || !c). Then !(b || !c) becomes !b && !!c = !b && c. So the equivalent is !a || (!b && c). Option A applies De Morgan to (b || !c) incorrectly (using || instead of &&); option B incorrectly turns the outer || into &&; option D is just !a && the original inner expression, which is wrong on both sides.
Answer: Correct: C
A
!a || (!b || c)
B
!a && (!b || c)
C
!a || (!b && c)
D
!a && (b || !c)
Answer images
Question 425
int n = 100;
int steps = 0;
while (n > 1)
{
if (n % 2 == 0)
{
n = n / 2;
}
else
{
n = n - 1;
}
steps = steps + 1;
}
System.out.println(steps);
What is printed when the code segment executes?Main Solution
Answer
Answer: B. Trace n after each iteration: 100 → 50 → 25 → 24 → 12 → 6 → 3 → 2 → 1. That is 8 updates before the loop condition n > 1 fails, so steps = 8. Option A forgets the final divide-by-two from 2 → 1; option C double-counts an iteration; option D treats the odd step as still halving.
Answer: Correct: B
A
7
B
8
C
9
D
10
Answer images
Question 426
Question 427
Question 428
public static String rate(int n)
{
if (n < 0 || n > 100)
{
return "invalid";
}
if (n < 25)
{
return "low";
}
if (n < 75)
{
return "mid";
}
return "high";
}
For how many integer values of n in the range \(0 \leq n \leq 100\) does rate(n) return "mid"?Main Solution
Answer
Answer: A. The method returns "mid" exactly when 25 ≤ n < 75: the first guard eliminates negatives and values above 100; the next guard eliminates n < 25; the next guard catches all remaining values below 75. The integer values 25, 26, …, 74 form a set of size 74 - 25 + 1 = 50. Option C drops the boundary 25; option B includes the boundary 75; option D miscounts both boundaries.
Answer: Correct: A
A
50
B
51
C
49
D
52
Answer images
Question 429
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.

