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 362
Question 363
Question 364
public static String g(int n)
{
if (n < 10)
{
return "" + n;
}
return (n % 10) + g(n / 10);
}
What is returned by the call g(1423)?Main Solution
Answer
Answer: D. g(1423) = "3" + g(142) = "3" + "2" + g(14) = "3" + "2" + "4" + g(1). Base: g(1) = "1". Concatenation yields "3" + "2" + "4" + "1" = "3241". The method outputs the digits of n in reverse order.
Answer: Correct: D
Answer images
Question 365
Question 366
Question 367
Question 368
int n = 128;
int steps = 0;
while (n > 1)
{
n = n / 2;
steps = steps + 1;
}
System.out.println(steps);
What is printed when the code segment executes?Main Solution
Answer
Answer: C. The loop halves n (using integer division) on every iteration and increments steps. Starting from 128: 128 → 64 → 32 → 16 → 8 → 4 → 2 → 1. That is 7 halvings, so steps ends at 7. The loop then exits because n > 1 is false when n == 1. Option B is obtained by stopping one step early; option D adds an extra division that would take n to 0, but the guard prevents it.
Answer: Correct: C
A
5
B
6
C
7
D
8
Answer images
Question 369
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.

