AP® Computer Science A Selection and Iteration Practice Questions
Work through AP Computer Science A practice 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
Question 192
int n = 100;
int count = 0;
while (n > 1)
{
n = n / 2;
count++;
}
System.out.println(count);Main Solution
Solution:
Correct: B. Trace of n: 100 → 50 → 25 → 12 → 6 → 3 → 1. The loop runs 6 times (the condition n>1 becomes false when n = 1).
Answer: Correct: B
Answer images
Question 193
int count = 0;
for (int i = 0; i < 5; i++)
{
for (int j = i; j < 5; j++)
{
count++;
}
}
System.out.println(count);Main Solution
Solution:
Correct: D. Inner iterations per i: i=0 → 5, i=1 → 4, i=2 → 3, i=3 → 2, i=4 → 1. Total = 5+4+3+2+1 = 15.
Answer: Correct: D
Answer images
Question 194
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
Question 195
int n = 1;
int total = 0;
while (total < 30)
{
total = total + n;
n = n + 2;
}
System.out.println(n + " " + total);Main Solution
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
Answer images
Question 196
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(i + "" + j + " ");
}
}
System.out.println();Main Solution
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
Answer images
Question 197
int count = 0;
for (int k = 1; k <= 50; k++)
{
if (k % 3 == 0 && k % 5 != 0)
{
count++;
}
}
System.out.println(count);Main Solution
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
Answer images
Question 198
A small neighborhood grocery store tracks how many customers are waiting in each of its checkout lanes. The store uses the class GroceryStore, whose skeleton is shown below.
public class GroceryStore
{
/** The number of customers currently waiting in each lane.
* laneQueues[k] stores the customers in lane k (0-based).
* The array is never null and has at least one element. */
private int[] laneQueues;
/** The number of seconds it takes to serve one customer
* in any lane. Always positive. */
private int secondsPerCustomer;
/** Constructs a GroceryStore with the given queue snapshot and
* per-customer service time. */
public GroceryStore(int[] queues, int secs)
{
/* not shown */;
}
/** Returns the estimated wait time, in seconds, for a customer
* who joins the back of lane laneIndex right now. Part (a) */
public int estimatedWait(int laneIndex)
{
/* part (a) */;
}
/** Returns the index of the lane with the shortest estimated wait.
* If multiple lanes are tied, the lowest such index is returned.
* Part (b) */
public int fastestLane()
{
/* part (b) */;
}
// Other methods and constructors are not shown.
}Part A
Write the method estimatedWait. A customer joining the back of a lane must wait for every customer already in that lane to be served plus the time to be served themselves. Each customer takes exactly secondsPerCustomer seconds.
For example, suppose secondsPerCustomer is 45 and laneQueues is {3, 1, 4, 2}. The following table shows the expected return values.
| Call | Returned Value | Explanation |
|---|---|---|
estimatedWait(0) |
180 | 3 ahead + 1 self = 4 × 45 |
estimatedWait(1) |
90 | 1 ahead + 1 self = 2 × 45 |
estimatedWait(2) |
225 | 4 ahead + 1 self = 5 × 45 |
estimatedWait(3) |
135 | 2 ahead + 1 self = 3 × 45 |
Assume laneIndex is a valid index in laneQueues.
Part B
Write the method fastestLane. It returns the index of the lane with the shortest estimated wait for a new customer joining now. If two or more lanes are tied for the minimum, return the smallest such index.
For example, if secondsPerCustomer is 30 and laneQueues is {2, 0, 2, 0, 3}, then the estimated waits are {90, 30, 90, 30, 120}. The minimum is 30, achieved by lanes 1 and 3, so fastestLane() returns 1.
You must call estimatedWait appropriately to receive full credit. Assume estimatedWait works as specified regardless of what you wrote in Part A.
Main Solution
Part A — estimatedWait
The formula is: (number of customers already in the lane + 1) × secondsPerCustomer. The "+1" accounts for the joining customer themselves.
public int estimatedWait(int laneIndex)
{
return (laneQueues[laneIndex] + 1) * secondsPerCustomer;
}
Explanation:
laneQueues[laneIndex]gives the number of customers currently waiting in lanelaneIndex.- The new customer must wait for all of those customers to be served, then be served themselves — that is
laneQueues[laneIndex] + 1total customers. - Multiplying by
secondsPerCustomerconverts the customer count to seconds.
Verification with the example (secondsPerCustomer = 45, laneQueues = {3, 1, 4, 2}):
estimatedWait(0): (3+1)×45 = 180 ✓estimatedWait(1): (1+1)×45 = 90 ✓estimatedWait(2): (4+1)×45 = 225 ✓estimatedWait(3): (2+1)×45 = 135 ✓
Part B — fastestLane
Iterate over all lanes, calling estimatedWait for each. Track the index of the lane with the current minimum. Because we initialize with lane 0 and update only when strictly less than the current minimum, ties are broken in favor of the lower index automatically.
public int fastestLane()
{
int bestIndex = 0;
int bestWait = estimatedWait(0);
for (int i = 1; i < laneQueues.length; i++)
{
int wait = estimatedWait(i);
if (wait < bestWait)
{
bestWait = wait;
bestIndex = i;
}
}
return bestIndex;
}
Explanation:
- We initialize
bestIndex = 0andbestWait = estimatedWait(0)before the loop. - For each subsequent lane index
i, we computeestimatedWait(i)and compare to the running minimum. - We update only when
wait < bestWait(strictly less), so the first (lowest-indexed) minimum is retained on ties. - After the loop,
bestIndexholds the index of the lane with the shortest wait.
Verification with the example (secondsPerCustomer = 30, laneQueues = {2, 0, 2, 0, 3}):
- Waits: {90, 30, 90, 30, 120}
- Minimum is 30, first achieved at index 1 → returns 1 ✓
Teacher Explanation
Part A tests the ability to access an array element and apply a simple arithmetic formula. Students must recognize that the new customer must wait for all existing customers plus themselves, giving (laneQueues[laneIndex] + 1) * secondsPerCustomer.
Part B tests the standard "find minimum index" pattern while requiring correct use of Part A's method. Full credit requires calling estimatedWait (not recomputing the formula inline). Tie-breaking in favor of the lowest index is achieved naturally by initializing to index 0 and updating only on strict improvement.
Education Coach Advice
AP CS A FRQ Strategy:
- Read the Javadoc carefully — the comments tell you exactly what the method must do.
- Always use the provided helper methods (Part A's
estimatedWaitin Part B) — graders explicitly reward this and penalize reimplementation. - For "find minimum" problems, initialize to the first element/index before the loop, then use strict
<for updating so ties favor the lower index. - After writing your answer, mentally trace through the given examples to self-check — the problem gives you test cases for free.
- Keep methods short and focused; avoid extra variables unless they improve clarity.
Traps
Part A common errors:
- Forgetting to add 1 for the joining customer — returning
laneQueues[laneIndex] * secondsPerCustomerinstead of(laneQueues[laneIndex] + 1) * secondsPerCustomer. - Using the wrong array index (off-by-one or using the wrong variable name).
Part B common errors:
- Not calling
estimatedWait(computing the formula inline loses credit). - Updating
bestIndexon<=instead of<, which would return the last minimum index rather than the first. - Initializing
bestWaittoInteger.MAX_VALUEbut forgetting to setbestIndexproperly, or starting the loop at index 0 causing double-evaluation (minor, not penalized but inelegant). - Using
laneQueues.length - 1as the loop bound and missing the last lane.
Answer images
Question 199
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?Main Solution
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
Answer images
Question 200
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?Main Solution
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
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.

