AP Computer Science A • Unit 2.9

Implementing Selection and Iteration Algorithms

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

Q1C
Q2B
Q3B
Q4D
Q5D
Q6A
Q7A
Q8B
Q9C
Q10B
Q14D
Q11B
Q12B
Q13C
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?

int sum = 0;

for (int value = 2; value <= 8; value += 2) {
    if (value > 4) {
        sum += value;
    }
}

System.out.println(sum);
A8
B12
C14
D20
Correct answer: C. The loop visits 2, 4, 6, and 8, and only 6 and 8 pass the test, giving 14.
Explanation

This combines selection with the accumulator pattern. The accumulator sum starts at 0 and is updated only when the current value qualifies. The guard value > 4 rejects 2 and 4, so the running total collects the two larger values.

  • A is not correct: only 8 is included by this answer, but 6 also satisfies value > 4 and must join the running total.
  • B is not correct: adding 2, 4, and 6 gives 12, but the condition rejects 2 and 4 while 8 still qualifies.
  • D is not correct: 20 is the total of every value the loop visits, which ignores the if statement and includes 2 and 4.
Q2

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

int total = 0;
int count = 0;

for (int value = 2; value <= 6; value += 2) {
    total += value;
    count++;
}

System.out.println(total / count);
A3
B4
C6
D12
Correct answer: B. The loop totals 2, 4, and 6 as 12 over a count of 3, giving 4.
Explanation

This is the average pattern. The loop builds total and increments count together on every iteration, so both describe the same group of values. The division happens once, after the loop, when the count is finally correct.

  • A is not correct: 3 is the value of count, the number of values included, rather than the result the division produces.
  • C is not correct: 6 is the last value the loop visits, not the result of dividing the running total by the count.
  • D is not correct: 12 is the value of total after the loop, before it is divided by count.
Q3

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

int count = 0;

for (int value = 2; value <= 14; value += 3) {
    if (value % 2 == 0) {
        count++;
    }
}

System.out.println(count);
A2
B3
C4
D10
Correct answer: B. The loop steps by 3 and visits 2, 5, 8, 11, and 14, of which three are even.
Explanation

This is the counter pattern with a condition. The variable count starts at 0 and increases only for values that satisfy value % 2 == 0. Because the loop increases by 3, the visited values alternate between even and odd.

  • A is not correct: 2 undercounts the even values, since 2, 8, and 14 all leave remainder 0 and each raises the counter.
  • C is not correct: the loop visits five values in total, and only three of them are even, so the counter cannot reach 4.
  • D is not correct: 10 is far larger than the number of iterations, and the counter increases at most once per visited value.
Q4

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

int n = 1205;
int copy = n;
int count = 0;

while (n > 0) {
    count++;
    n /= 10;
}

System.out.println(copy + " " + n + " " + count);
A1205 1205 4
B1205 0 3
C0 1205 4
D1205 0 4
Correct answer: D. copy keeps the original 1205, the loop leaves n at 0, and count reaches 4.
Explanation

This is the digit-counting pattern. Each iteration removes the last digit through integer division, so the loop body runs once per digit and ends with n equal to 0. Saving a copy first is what keeps the original value available.

  • A is not correct: the loop changes n itself on every iteration, so n cannot still hold 1205 once the loop finishes.
  • B is not correct: 1205 has four digits, so the loop body runs four times and count ends at 4 rather than 3.
  • C is not correct: copy is assigned before the loop and is never changed afterwards, so it still stores 1205 instead of 0.
Q5

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

int max = -18;

for (int value = -15; value <= -3; value += 4) {
    if (value > max) {
        max = value;
    }
}

System.out.println(max);
A-18
B-15
C-7
D-3
Correct answer: D. The loop steps by 4 from -15 up to -3, and -3 is the largest value it visits.
Explanation

This is the maximum pattern. The variable max holds the best value found so far and is replaced whenever a larger value appears. Every visited value is greater than the starting -18, so the final result is the largest one.

  • A is not correct: -18 is only the starting value, and the loop finds values greater than it, so max is replaced.
  • B is not correct: -15 is the first value the loop visits and the smallest one, so later larger values replace it.
  • C is not correct: -7 is one of the values visited, but the loop does not stop there, and a larger value replaces it.
Q6

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

int min = Integer.MAX_VALUE;

for (int value = 12; value >= 3; value -= 3) {
    if (value < min) {
        min = value;
    }
}

System.out.println(min);
A3
B6
C9
D12
Correct answer: A. The loop counts down to 3, and each visited value is smaller than the previous best, so min ends at 3.
Explanation

This is the minimum pattern. Starting min at Integer.MAX_VALUE guarantees that the first real value replaces it, and every smaller value replaces the best so far. The loop decreases by 3, so the last value visited is also the smallest.

  • B is not correct: 6 is visited, but the loop continues while the control value stays at least 3 and then finds a smaller value.
  • C is not correct: 9 is the second value visited, and the loop still has smaller values left to compare against min.
  • D is not correct: 12 is the first value visited, so it becomes the best so far immediately, but smaller values replace it later.
Q7

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

boolean allOdd = true;

for (int value = 3; value <= 15; value += 3) {
    if (value % 2 == 0) {
        allOdd = false;
    }
}

System.out.println(allOdd);
Afalse
Btrue
C15
DThe code does not compile.
Correct answer: A. The loop visits 6 and 12, which are even, so the flag allOdd is set to false.
Explanation

This is the all-match pattern. The Boolean flag begins as true and changes to false as soon as one value fails the test. Nothing in this loop sets the flag back, so one failing value settles the printed result.

  • B is not correct: true would be printed only if every visited value were odd, but 6 and 12 are even.
  • C is not correct: the variable printed is allOdd, a boolean flag, so the output is a logical value rather than a number.
  • D is not correct: the code is valid, because a boolean variable may be assigned false inside an if statement and then printed.
Q8

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

int n = 9;
boolean prime = n >= 2;

for (int divisor = 2; divisor < n; divisor++) {
    if (n % divisor == 0) {
        prime = false;
    }
}

System.out.println(prime);
Atrue
Bfalse
C9
DThe code does not compile.
Correct answer: B. 9 is divisible by 3, so the test inside the loop sets prime to false.
Explanation

This is the prime-checking pattern written with a Boolean flag instead of an immediate return. The flag begins as the result of n >= 2, and any divisor that divides n evenly changes the flag to false.

  • A is not correct: true would require that no divisor below n divides 9 evenly, but 3 does divide it.
  • C is not correct: the value printed is prime, a boolean flag, rather than the number stored in n.
  • D is not correct: the code compiles, since n >= 2 is a valid Boolean expression and the loop and remainder test are legal.
Q9

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

int number = 527;
int digit = number % 10;

System.out.println(digit);
A2
B5
C7
D52
Correct answer: C. For a positive integer, number % 10 gives the last digit, and the last digit of 527 is 7.
Explanation

This is the digit-processing rule. The remainder operator used with 10 isolates the last digit, while integer division by 10 would remove it instead. Only the remainder is computed here, so the stored value is the final digit.

  • A is not correct: 2 is the middle digit of 527, and the remainder with 10 reaches only the last digit.
  • B is not correct: 5 is the leading digit, which the remainder operator does not reach in a single step.
  • D is not correct: 52 is what number / 10 produces, the number with its last digit removed rather than the digit itself.
Q10

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

int total = 0;

for (int value = 1; value <= 5; value++) {
    if (value % 2 == 1) {
        total += value;
    }
}

System.out.println(total);
A6
B9
C15
D25
Correct answer: B. The condition selects the odd values 1, 3, and 5, whose total is 9.
Explanation

This combines a divisibility test with the accumulator pattern. A value leaves remainder 1 when divided by 2 exactly when it is odd, and total collects only the values that pass that test.

  • A is not correct: 6 is the total of the even values, which are the ones the condition rejects rather than accepts.
  • C is not correct: 15 is the total of every value from 1 through 5, which ignores the if statement.
  • D is not correct: 25 is larger than the total of all the values the loop visits, so no selection can produce it.
Q14

Consider the following method. What value is returned by the call firstMultiple(9)?

public static int firstMultiple(int start) {
    for (int n = start; n <= 15; n++) {
        if (n % 4 == 0) {
            return n;
        }
    }
    return -1;
}
A-1
B9
C15
D12
Correct answer: D. The loop starts at 9 and returns the first value divisible by 4, which is 12.
Explanation

This is the first-match pattern. The method returns as soon as a candidate satisfies the requirement, and the failure result sits after the loop so that every candidate gets a chance to be tested.

  • A is not correct: -1 is returned only when the loop finishes with no match, and 12 matches before the loop ends.
  • B is not correct: 9 is the starting value, but it is returned only if it satisfies n % 4 == 0, and it does not.
  • C is not correct: 15 is the loop bound rather than the first qualifying value, and the method stops at the earliest match.
Q11

Assume that d is not 0. Which expression correctly tests whether n is evenly divisible by d?

An / d == 0
Bn % d == 0
Cn % d == 1
Dn * d == 0
Correct answer: B. The remainder operator tests divisibility, and n % d == 0 is true exactly when d divides n evenly.
Explanation

Divisibility is checked with the remainder rather than the quotient. When one value divides another evenly, the remainder expression produces 0, and the comparison with 0 is what detects that.

  • A is not correct: n / d is integer division, so this compares the quotient rather than the remainder that divisibility depends on.
  • C is not correct: a remainder of 1 shows that the division is not even, so this expression tests the opposite of divisibility.
  • D is not correct: n * d multiplies the two values, and the product says nothing about whether one divides the other evenly.
Q12

Which initialization is most appropriate before finding the largest value in a sequence of int values that may all be negative?

Aint max = 0;
Bint max = Integer.MIN_VALUE;
Cint max = Integer.MAX_VALUE;
Dint max = 1;
Correct answer: B. Starting max at Integer.MIN_VALUE lets the first real value replace it, even when every value is negative.
Explanation

A best-so-far result must begin with a value that the real data cannot defeat. Initializing max to 0 fails when every value is negative, because no negative value is ever larger than the starting result.

  • A is not correct: int max = 0; leaves 0 as the reported answer when every value is negative, since none of them is larger.
  • C is not correct: Integer.MAX_VALUE is the sensible start for a minimum rather than a maximum, because no value would ever be larger.
  • D is not correct: int max = 1; has the same flaw as starting at 0, and it also rules out every value below it.
Q13

A loop averages only values that meet a condition. Which updates should occur inside that condition?

AUpdate only sum.
BUpdate only count.
CUpdate both sum and count.
DUpdate neither variable.
Correct answer: C. Both sum and count must be updated inside the condition so that they describe the same group of values.
Explanation

An average needs a total and a count that match. If the two updates sit in different places, the count includes values the total never added, and the division mixes two different groups of values.

  • A is not correct: updating only sum leaves count describing a larger group, so the division uses a count that does not match the total.
  • B is not correct: updating only count means the total never grows with the selected values, so the division no longer describes them.
  • D is not correct: updating neither variable leaves both at their starting values, so the loop builds nothing for the division to use.