Questions: Nested Loops and Multi-Level Iteration

5 questions to test your understanding

Score: 0 / 5
Question 1 Multiple Choice

Consider this nested loop: for i in range(3), and inside it for j in range(2), printing (i, j) each iteration. How many lines of output does this produce, and what is the last line printed?

A3 lines; last line is (2, 2)
B5 lines; last line is (2, 1)
C6 lines; last line is (2, 1)
D6 lines; last line is (3, 2)
Question 2 Multiple Choice

A nested loop has an outer loop that runs 5 times and an inner loop that runs 4 times. How many times does the body of the inner loop execute in total?

A9 times (5 + 4)
B20 times (5 × 4)
C16 times (4 squared)
DIt depends on the specific loop conditions at runtime
Question 3 True / False

In a nested loop, the inner loop's counter variable retains its final value from the previous outer iteration when the outer loop advances to the next iteration.

TTrue
FFalse
Question 4 True / False

Nested loops are the natural structure for generating every combination of items from two independent sets.

TTrue
FFalse
Question 5 Short Answer

Why does the body of the inner loop execute outer × inner times rather than outer + inner times?

Think about your answer, then reveal below.