AP Computer Science A • Unit 2.11

Nested Iteration

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

Q1D
Q2B
Q3D
Q4B
Q5C
Q6A
Q7A
Q8A
Q9C
Q10B
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 count = 0;

for (int r = 1; r <= 3; r++) {
    for (int c = 1; c <= 4; c++) {
        count++;
    }
}

System.out.println(count);
A3
B4
C7
D12
Correct answer: D. The outer loop runs three times and the inner loop runs four times for each of those, so count reaches 12.
Explanation

For a rectangular nested loop, the inner body executes the number of outer iterations times the number of inner iterations. Here the outer header allows r values 1 through 3, and each of those runs the inner loop over four c values.

  • A is not correct: this is only the number of outer iterations. The inner body runs once for every c value inside every r value, not once per row.
  • B is not correct: four is the number of inner iterations for a single outer iteration, but the inner loop starts over and runs again for each new value of r.
  • C is not correct: this adds the two loop counts instead of multiplying them, and the total for a rectangular nested loop comes from multiplying outer iterations by inner iterations.
Q2

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

for (int n = 1; n <= 3; n++) {
    for (int i = 1; i <= n; i++) {
        System.out.print(i);
    }

    System.out.println();
}
A123 on each of three lines
B1, then 12, then 123
C123, then 12, then 1
DOne 1 only
Correct answer: B. Because the inner bound depends on n, the first line prints 1, the second 12, and the third 123.
Explanation

This is a triangular pattern: the inner header uses the outer-loop variable as its bound, so the inner loop runs one more time on each pass. The call to System.out.println() placed after the inner loop ends one complete line.

  • A is not correct: printing 123 three times would require a fixed inner bound. The bound here is n, which changes with every outer iteration.
  • C is not correct: that order shrinks from the longest line to the shortest, which happens when the outer variable decreases. Here n increases from 1 to 3.
  • D is not correct: the inner loop does not stop after the first outer iteration. Its control variable is initialized again at the inner header, so it runs to completion for each value of n.
Q3

Consider the following code segment. How many lines are printed as a result of executing the code segment?

for (int d = 1; d <= 2; d++) {
    for (int s = 1; s <= 3; s++) {
        System.out.println("X");
    }
}
A2
B3
C5
D6
Correct answer: D. The call to System.out.println("X") sits inside the inner loop, so it runs once for each of the six inner iterations.
Explanation

The outer loop runs twice, and for each of those the inner loop runs three times, which gives 6 executions of the print statement. Because the statement is println, every execution ends a line of its own.

  • A is not correct: two is the number of outer iterations only, and the inner loop runs from start to finish inside each one, so more lines appear.
  • B is not correct: three is the number of inner iterations within a single outer iteration, and the inner loop starts over for the second value of d.
  • C is not correct: this adds the outer and inner counts, while the total for a rectangular nested loop comes from multiplying them instead.
Q4

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

int count = 0;

for (int r = 1; r < 3; r++) {
    for (int c = 1; c <= 3; c++) {
        count++;
    }
}

System.out.println(count);
A3
B6
C9
D12
Correct answer: B. The outer header stops before 3, so only two passes of three inner iterations run and count ends at 6.
Explanation

A header that uses < stops one value earlier than a header that uses <=, because the bound value itself never gets a turn. Here r takes the values 1 and 2, and each runs the inner loop three times.

  • A is not correct: three is the number of inner iterations for one outer iteration, but the inner loop is initialized again and runs a second time.
  • C is not correct: this treats the outer header as letting r reach 3. The strict bound stops one value earlier, so only two passes run.
  • D is not correct: twelve would require four outer iterations of three inner iterations, and the outer header here permits only two values of r.
Q5

Consider the following code segment. How many values are printed as a result of executing the code segment?

for (int g = 2; g <= 4; g++) {
    for (int i = 1; i < g; i++) {
        System.out.print(i);
    }
}
A3
B5
C6
D9
Correct answer: C. The inner bound depends on g, so the outer iterations print one, two, and three values, for 6 values in all.
Explanation

This is a triangular pattern whose inner header stops one value before g. The number of printed values grows as g grows, so the total is the sum of those counts rather than a single product.

  • A is not correct: three is the number of outer iterations, and each outer iteration prints at least one value, so the total number of printed values is larger.
  • B is not correct: five undercounts the pattern by one, since the three outer iterations print one, two, and three values, which comes to 6.
  • D is not correct: nine assumes a rectangular pattern with a fixed inner count, but the inner bound here depends on g, so the counts differ.
Q6

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

for (int r = 1; r <= 2; r++) {
    for (int c = 1; c <= 2; c++) {
        System.out.print(r + "" + c + " ");
    }
}
A11 12 21 22
B11 21 12 22
C12 11 22 21
D1 2 1 2
Correct answer: A. The outer value stays fixed while the inner loop finishes, so both items beginning with 1 print before those beginning with 2.
Explanation

For each single iteration of the outer loop, the inner loop runs from start to finish. That produces 11 12 for the first outer value, after which the inner loop starts over and produces 21 22.

  • B is not correct: that order would need the second digit to stay fixed while the first one changes, which is not how the inner loop advances inside one outer iteration.
  • C is not correct: this counts the inner variable downward. The inner header starts at 1 and increases, so 11 is printed before 12.
  • D is not correct: the printed text includes both control variables rather than the inner one alone, so each item shows an outer value followed by an inner value.
Q7

When execution reaches the inner for loop header during a new outer-loop iteration, what happens?

AIts control variable is initialized and the inner loop runs again.
BIt continues with the final control-variable value from the previous outer iteration.
CIt is skipped after the first outer iteration.
DThe outer-loop variable is initialized again.
Correct answer: A. Reaching the inner header again initializes the inner control variable, so the inner loop starts over and runs to completion.
Explanation

Each time the outer loop comes back around, the inner loop starts over from scratch. The trace of the rectangular example shows the reset: after row = 1 reaches col = 5, the next pair is row = 2 and col = 1, not col = 6.

  • B is not correct: the control variable does not keep its final value from the previous outer iteration, because the inner header initializes it again for the new row.
  • C is not correct: the inner loop is not skipped after the first pass, since for each single iteration of the outer loop it runs from start to finish.
  • D is not correct: the inner header initializes the inner control variable, not the outer one, which keeps its current value while the inner loop finishes.
Q8

An outer loop uses group as its control variable. Which inner-loop condition can make the number of inner iterations change with group?

Aitem <= group
Bitem <= 4
Citem < 5
Ditem != 0
Correct answer: A. item <= group ties the inner bound to the outer-loop variable, which is what makes a nested loop triangular.
Explanation

A triangular nested loop has an inner bound that depends on the outer-loop variable. When the bound is that variable itself, the inner loop runs a different number of times for each outer iteration instead of a fixed number.

  • B is not correct: a fixed bound of 4 gives the same number of inner iterations for every outer iteration, which is the rectangular pattern rather than the triangular one.
  • C is not correct: this bound is also fixed, so the inner count stays the same no matter which value group currently holds.
  • D is not correct: this condition does not refer to group at all, so the number of inner iterations cannot change from one outer iteration to the next.
Q9

What is printed by this code segment?

for (int g = 3; g >= 1; g--) {
    for (int i = 1; i <= g; i++) {
        System.out.print(i);
    }
    System.out.println();
}
A123 on each of three lines
B1, then 12, then 123
C123, then 12, then 1
DOne 1 only
Correct answer: C. The outer variable decreases from 3 and the inner bound follows it, so each printed line is one digit shorter than the last.
Explanation

A triangular pattern can also shrink. The outer header starts at the larger value and decreases after each outer iteration, while the inner bound stays tied to the outer-loop variable, so the longest line is printed first.

  • A is not correct: identical lines would require a fixed inner bound, and the bound here is g, which changes with every outer iteration.
  • B is not correct: that order grows from the shortest line to the longest, which happens when the outer variable increases rather than decreases.
  • D is not correct: the inner loop is initialized again at its header for each outer iteration, so it prints a full line every time instead of running only once.
Q10

How many times is "V" printed?

for (int f = 2; f <= 5; f++) {
    for (int r = 0; r < 3; r++) {
        System.out.print("V");
    }
}
A7
B12
C15
D20
Correct answer: B. The outer loop runs four times and the inner loop runs three times for each of those, so "V" is printed 12 times.
Explanation

Count the values each header allows rather than reading the bounds directly. The outer header covers 2 through 5, which is four values, and the inner header allows three values of r before its strict bound stops it.

  • A is not correct: seven adds the two counts, while the total for a rectangular nested loop comes from multiplying the outer count by the inner count.
  • C is not correct: fifteen would require five outer iterations of three inner iterations, but the outer header starts at 2, so it allows only four values.
  • D is not correct: twenty multiplies four by five, and the inner header stops before 3, so it allows three iterations rather than five.