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 172
!(x > 5 && y != 2) ?Main Solution
Answer
Answer: A. By De Morgan's law, negating P && Q gives !P || !Q. Here, !(x > 5) becomes x <= 5 and !(y != 2) becomes y == 2.
Answer: Correct: A
A
x <= 5 || y == 2
B
x < 5 && y == 2
C
x <= 5 && y == 2
D
x > 5 || y != 2
Answer images
Question 173
int x = 4;
int y = 9;
boolean b = x > 5 || y / x == 2;
What is the value of b after the code segment executes?Main Solution
Answer
Answer: C. x > 5 is false, so the second part is evaluated. Because y / x uses integer division, 9 / 4 is 2, so y / x == 2 is true. Therefore b is true.
Answer: Correct: C
A
A run-time error occurs.
B
false
C
true
D
The value cannot be determined.
Answer images
Question 174
Question 175
Question 176
Question 177
Question 178
n is outside the range 5 through 12, inclusive?Main Solution
Answer
Answer: B. Values outside the inclusive interval [5, 12] are those that are either less than 5 or greater than 12.
Answer: Correct: B
A
n < 5 && n > 12
B
n < 5 || n > 12
C
n <= 5 || n >= 12
D
!(n < 5 || n > 12)
Answer images
Question 179
Question 180
public static int process(int n)
{
int count = 0;
while (n >= 10)
{
if (n % 2 == 0)
n = n / 2;
else
n = n - 1;
count++;
}
return count;
}
process(19) ?Main Solution
Answer
Answer: A. Since 19 is odd, it becomes 18. Then 18 is even, so it becomes 9. At that point the loop stops because 9 < 10. The method returns 2.
Answer: Correct: A
A
2
B
3
C
1
D
4
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.

