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.
Question 412
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 413
Question 414
Question 415
Question 416
Question 417
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
Question 418
Question 419
Question 420
Consider the following code segment.
int x = 4;
int y = 6;
if (x + y > 10)
{
x = y - x;
}
else
{
y = x + y;
}
if (x * 2 >= y)
{
y = y - x;
}
else
{
x = x + y;
}
System.out.println(x + " " + y);
What is printed?
Main Solution
Answer
Answer: D. The first condition is false because 4 + 6 is not greater than 10, so y becomes 10. Then x * 2 is 8, which is less than 10, so x becomes 14.
Answer: Correct: D
A
10 6
B
4 10
C
2 4
D
14 10
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.

