AP Computer Science A • Unit 2.8

for Loops

Detailed answer explanations for the Unit 2.8 multiple-choice practice questions.

Q1A
Q2A
Q3C
Q4B
Q5C
Q6C
Q7D
Q8C
Q9B
Q10B
Q11C
Q12A
How to use this page: First try each question without looking at the explanation. Then compare your answer with the key and read why the other choices are not correct.

Answer Explanations

Q1

Consider the following code segment. What is printed as a result of executing the code segment?

for (int i = 3; i <= 10; i += 3) {
    System.out.print(i + " ");
}
A3 6 9
B3 6 9 12
C2 5 8
DThe loop is infinite.
Correct answer: A. The counter starts at 3 and increases by 3, so it takes the values 3, 6, and 9.
Explanation

A for loop checks its condition before every iteration. After 9 is printed, the update i += 3 raises the counter to 12, and i <= 10 is then false, so the loop stops without printing that value.

  • B is not correct: the counter does reach 12, but the condition is tested before the body runs, so the loop ends without printing 12.
  • C is not correct: an update of i += 3 starting from 3 never produces 2, 5, or 8; that sequence begins at a different value.
  • D is not correct: the update moves the counter toward the value that makes the condition false, so the loop makes progress and stops after three passes.
Q2

Consider the following code segment. What is printed as a result of executing the code segment?

int count = 0;

for (int i = 5; i < 5; i++) {
    count++;
}

System.out.println(count);
A0
B1
C5
DThe loop is infinite.
Correct answer: A. The condition i < 5 is false at the very first check, so the body never runs and count stays 0.
Explanation

A for loop tests its condition before every iteration, including the first. When that first test fails, the body never runs and the loop executes zero times. The variable declared before the loop keeps the value it was given.

  • B is not correct: the body would have to run once to reach count++, but the first test of i < 5 already fails.
  • C is not correct: 5 is the starting value of the loop control variable, not the number of times the body runs.
  • D is not correct: an infinite loop keeps satisfying its condition; here the condition is false immediately, so the loop stops right away.
Q3

Consider the following code segment. What is printed as a result of executing the code segment?

int result = 1;

for (int i = 3; i < 6; i += 2) {
    result *= i;
}

System.out.println(result);
A3
B5
C15
D30
Correct answer: C. The loop runs with i equal to 3 and then 5, so result becomes 1 times 3 times 5.
Explanation

A variable declared before the loop can carry a value across iterations. The update i += 2 gives two passes here, because the next counter value 7 fails i < 6, and each pass multiplies the running value.

  • A is not correct: 3 is the value after the first pass only; the loop runs again with i equal to 5.
  • B is not correct: 5 is the second value of the counter, not the product that result holds after both passes.
  • D is not correct: 30 would require an extra factor of 2; the starting value of result is 1.
Q4

Consider the following code segment. What is printed as a result of executing the code segment?

for (int i = 5; i >= 1; i -= 2) {
    System.out.print(i + " ");
}
A5 4 3 2 1
B5 3 1
C1 3 5
DThe loop is infinite.
Correct answer: B. The counter starts at 5 and decreases by 2 on each pass, giving 5, 3, and 1.
Explanation

A for loop can count down by decreasing the loop control variable. After 1 is printed, the update i -= 2 produces -1, which fails i >= 1, so the loop ends there.

  • A is not correct: that sequence would need a step of one; the update i -= 2 skips 4 and 2 entirely.
  • C is not correct: the values are correct but the order is reversed; the counter begins at 5 and moves downward from there.
  • D is not correct: the update moves the counter toward a value that makes i >= 1 false, so the loop stops after three passes.
Q5

Consider the following code segment. Which statement best describes the result?

for (int i = 4; i >= 1; i++) {
    System.out.print(i);
}
AThe loop prints 4 3 2 1.
BThe loop body never executes.
CThe loop is infinite because i moves upward while the condition remains true.
DThe code does not compile because i++ cannot be used with >=.
Correct answer: C. The counter starts at 4 and i++ moves it upward, so i >= 1 never becomes false.
Explanation

A for loop becomes infinite when its update never moves the loop control variable toward a value that makes the condition false. Increasing a counter that already sits above its lower bound leaves the condition satisfied forever. This is a logic error.

  • A is not correct: printing those values would require an update that counts down, such as i--, rather than i++.
  • B is not correct: the condition is true at the first check, because 4 is greater than or equal to 1, so the body runs.
  • D is not correct: the header compiles; nothing prevents an increasing update from being paired with >=, which is why this mistake is a logic error.
Q6

Consider the following code segment. What is printed as a result of executing the code segment?

int sum = 2;

for (int i = 2; i <= 6; i += 2) {
    sum += i;
}

System.out.println(sum);
A8
B12
C14
D16
Correct answer: C. The total begins at 2 and the loop adds 2, 4, and 6, which gives 14.
Explanation

A loop can build a total. Declare the total before the loop, update it inside the body on every pass, and use it after the loop ends. The starting value counts toward the result along with everything the loop adds.

  • A is not correct: 8 is the total after the pass with i equal to 4, so it leaves out the final pass.
  • B is not correct: 12 is the sum of the values the loop adds, but it omits the starting value 2 of sum.
  • D is not correct: 16 adds one value too many; the update i += 2 stops after 6 because 8 fails i <= 6.
Q7

Consider the following code segment. Which statement best describes the result?

for (int i = 0, i < 3, i++) {
    System.out.print(i);
}
AThe code prints 012.
BThe code prints 123.
CThe loop is infinite.
DThe code does not compile because the header parts must be separated by semicolons.
Correct answer: D. The three header parts must be separated by two semicolons, and this header uses commas instead, so it does not compile.
Explanation

The parentheses of a for header hold three parts, the initialization, the condition, and the update, separated by two semicolons. Counting them is a quick check: three parts, two semicolons. Commas do not separate those parts.

  • A is not correct: those values would be right for a working header, but this header cannot compile, so nothing is printed at all.
  • B is not correct: the counter starts at 0, and in any case the header does not compile, so no output is produced.
  • C is not correct: an infinite loop is a logic error in code that compiles; this header is rejected before the program can run.
Q8

In a for loop, when does the update execute?

ABefore initialization
BBefore the body every time
CAfter the body
DOnly after the loop ends
Correct answer: C. The update runs after each completed iteration of the body, and the condition is then checked again.
Explanation

The loop runs the initialization once, checks the condition, runs the body if the condition is true, runs the update, and returns to the condition. The update therefore follows the body rather than preceding it.

  • A is not correct: the initialization runs first and only once, before any test of the condition and before any update at all.
  • B is not correct: the condition, not the update, is what is checked before the body; the update comes after the body instead.
  • D is not correct: the update runs after every completed iteration, not once at the end; it is what moves the counter on each pass.
Q9

Which header produces counter values 2 5 8?

Afor (int i = 2; i < 8; i += 3)
Bfor (int i = 2; i <= 8; i += 3)
Cfor (int i = 2; i <= 8; i++)
Dfor (int i = 8; i >= 2; i -= 3)
Correct answer: B. Starting at 2 and stepping by 3, the counter takes 2, 5, and 8 because 8 satisfies i <= 8.
Explanation

The initialization, condition, and update must work together. Whether the final value belongs in the sequence depends on the bound, because i < n and i <= n include different final values. The bound has to be chosen deliberately rather than out of habit.

  • A is not correct: i < 8 excludes the final value, so the counter takes only 2 and 5.
  • C is not correct: i++ steps by one, so the counter takes every value from 2 through 8 rather than every third one.
  • D is not correct: that header counts down from 8, so the values appear in the opposite order.
Q10

Which sequence correctly describes one normal iteration after initialization in a for loop?

AUpdate, body, condition
BCondition, body, update
CBody, initialization, condition
DCondition, update, body
Correct answer: B. After the initialization runs once, each iteration checks the condition, runs the body, and then runs the update.
Explanation

The order is fixed: run the initialization once, check the condition, run the body if the condition is true, run the update, and return to the condition. Tracing a header in that order shows how many times the body runs.

  • A is not correct: the update follows the body rather than opening the iteration, and the condition is checked before the body, not after it.
  • C is not correct: the initialization runs once before the loop begins, so it cannot appear in the middle of an iteration.
  • D is not correct: the update runs after the body, not between the condition and the body; a passing test leads straight into the body.
Q11

When is a for loop generally the clearest choice?

AWhen the loop must run forever
BWhen the update is unknown until user input
CWhen repetition is controlled by a counter
DWhen the body must execute at least once
Correct answer: C. A for loop is clearest when the number of repetitions is already known or a counter drives the work.
Explanation

The header of a for loop groups the initialization, the condition, and the update in one place, which makes a counter-controlled plan for repetition visible before the body starts.

  • A is not correct: a loop that never stops is a logic error, not a reason to prefer a for loop over a while loop.
  • B is not correct: the header states the update in advance, so this describes work whose amount of repetition is not known beforehand.
  • D is not correct: a for loop tests its condition before every iteration, including the first, so the body may run zero times.
Q12

When rewriting a for loop as a while loop, where do the initialization and update go?

AInitialization before the loop; update at the end of the body
BBoth before the loop
CInitialization inside the body; update in the condition
DBoth after the loop
Correct answer: A. The initialization moves to a line before the loop, and the update becomes the last statement of the loop body.
Explanation

Any counting for loop can be rewritten as an equivalent while loop, and the rule follows the header. The condition becomes the while condition, while the other two parts move to the positions where they still run in the same order.

  • B is not correct: the update has to run after each pass through the body, so placing it before the loop would run it only once.
  • C is not correct: the initialization belongs before the loop so that it runs once, and a condition tests a value rather than updating it.
  • D is not correct: both parts have to run while the loop is still going; placed afterwards, neither the counter nor the test would work.