Question 191
int sum = 0;
for (int k = 2; k <= 20; k += 3)
{
sum += k;
}
System.out.println(sum);Main Solution
Solution:
Correct: B. Values of k added: 2, 5, 8, 11, 14, 17, 20. Sum = 2+5+8+11+14+17+20 = 77.
Answer: Correct: B
Answer images
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: Iteration Clear filter
int sum = 0;
for (int k = 2; k <= 20; k += 3)
{
sum += k;
}
System.out.println(sum);Solution:
Correct: B. Values of k added: 2, 5, 8, 11, 14, 17, 20. Sum = 2+5+8+11+14+17+20 = 77.
Answer: Correct: B
int n = 1;
int total = 0;
while (total < 30)
{
total = total + n;
n = n + 2;
}
System.out.println(n + " " + total);Solution:
Correct: D. Iterations (total, n) after body: (1,3)(4,5)(9,7)(16,9)(25,11)(36,13). Loop exits when total = 36 ≥ 30. Output: 13 36.
Answer: Correct: D
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(i + "" + j + " ");
}
}
System.out.println();Solution:
Correct: B. Inner runs 1, 2, then 3 times. i=1: "11". i=2: "21 22". i=3: "31 32 33". Output: 11 21 22 31 32 33.
Answer: Correct: B
int count = 0;
for (int k = 1; k <= 50; k++)
{
if (k % 3 == 0 && k % 5 != 0)
{
count++;
}
}
System.out.println(count);Solution:
Correct: D. Multiples of 3 up to 50: 16 values. Among them, also divisible by 5 (and hence by 15): 15, 30, 45 — three values. 16 − 3 = 13.
Answer: Correct: D
int count = 0;
for (int i = 1; i < 10; i++)
{
for (int j = i; j < 10; j += 2)
{
count++;
}
}
What value is stored in count after the code segment executes?Solution:
Answer: C. For each i, the inner loop iterates over values i, i+2, i+4, … below 10. That gives iteration counts of 5, 4, 4, 3, 3, 2, 2, 1, 1 for i = 1, 2, 3, 4, 5, 6, 7, 8, 9 — odd starting i takes odd steps to 9, even starting i stops at 8. Summing: 5 + 4 + 4 + 3 + 3 + 2 + 2 + 1 + 1 = 25.
Answer: Correct: C
for (int i = 0; i < 4; i++)
{
for (int j = 0; j <= i; j++)
{
System.out.print("*");
}
System.out.println();
}
What is printed when the code segment executes?Solution:
Answer: A. The outer loop runs with i = 0, 1, 2, 3. The inner loop runs i + 1 times (because j ranges over 0, 1, …, i), printing that many stars. Line 1 has one star, line 2 has two, line 3 has three, line 4 has four — a left-aligned increasing triangle. Choice D omits the last row, which would require i < 3.
Answer: Correct: A
int n = 50;
int k = 0;
while (n >= 1)
{
n = n / 2;
k++;
}
System.out.println(k);
What is printed when the code segment executes?Solution:
Answer: D. The values of n go 50 → 25 → 12 → 6 → 3 → 1 → 0 (integer division truncates toward zero). k is incremented once per pass, and the loop runs while n >= 1, so it also runs on the pass that takes n from 1 to 0. That is six iterations total, so k = 6.
Answer: Correct: D
int n = 0;
int m = 1;
while (m < 1000)
{
m = m * 2;
n++;
}
System.out.println(n);
What is printed when the code segment executes?Solution:
Answer: B. m doubles each pass: 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024. Each transition corresponds to one increment of n. The condition m < 1000 becomes false after m becomes 1024, but that last iteration has already executed, so n has been incremented ten times. Note that the check happens before the body; the body is not interrupted mid-iteration.
Answer: Correct: B
int x = 100;
int count = 0;
while (x > 1)
{
if (x % 3 == 0)
{
x = x / 3;
}
else
{
x = x - 1;
}
count++;
}
System.out.println(count);
What is printed when the code segment executes?Solution:
Answer: B. Each pass either divides by 3 (when a multiple of 3) or subtracts 1. Trace: 100 → 99 (subtract; not divisible by 3), 99 → 33, 33 → 11, 11 → 10, 10 → 9, 9 → 3, 3 → 1. That is seven transitions, so the loop exits with count = 7.
Answer: Correct: B
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.