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 402
int n = 81;
int count = 0;
while (n > 1)
{
if (n % 3 == 0)
n = n / 3;
else
n = n - 1;
count++;
}
What value is stored in count after the loop terminates?Main Solution
Answer
Answer: A. Because each value remains divisible by 3, the path is 81 → 27 → 9 → 3 → 1. That is 4 iterations.
Answer: Correct: A
A
4
B
5
C
3
D
6
Answer images
Question 403
Question 404
public static int process(int n)
{
int result = 0;
while (n > 0)
{
result = result + n % 10;
n = n / 100;
}
return result;
}
What value is returned by process(58364) ?Main Solution
Answer
Answer: C. The method adds the last digit, then removes the last two digits. Starting with 58364, it adds 4, then 3 from 583, then 5 from 5. The total is 12.
Answer: Correct: C
A
26
B
17
C
12
D
10
Answer images
Question 405
Question 406
Question 407
Question 408
Question 409
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.

