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: Boolean Expressions Clear filter
Question 162
Question 163
Question 164
Question 165
Question 166
Question 167
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 168
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 169
Question 170
arr is a non-null int array (possibly empty).
int i = 0;
while (i < arr.length && arr[i] != 0)
{
i = i + 1;
}
Which of the following statements about the loop is true for every valid value of arr?Main Solution
Answer
Answer: A. Short-circuit evaluation of && guarantees the bound check i < arr.length is performed before the index arr[i]. The loop terminates either because i reached arr.length (no exception, since indexing was skipped), or because arr[i] equaled 0 (the index i was valid). Option C fails for an empty array; option B is wrong precisely because of short-circuit evaluation; option D is wrong because reaching the end of the array also terminates the loop.
Answer: Correct: A
A
After the loop, either i == arr.length or arr[i] == 0, and no exception is thrown.
B
If arr.length == 0, the loop throws an ArrayIndexOutOfBoundsException.
C
The loop body always executes at least once.
D
The loop terminates only if arr contains a zero.
Answer images
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.

