Answer key · 12 questions

Python Generators Exercises With Answers

All 12 questions for Python Generators in one place. Try the practice page first, then use this page to check the exact answer and the reason.

Quick answer

This answer key contains 4 multiple-choice, 4 fill-in-the-blank, and 4 output-prediction questions for Python Generators, with the exact answer and a short explanation for every question.

The answers are shown in full, so use this page after making an attempt. The practice page is the better first pass.

01 · Multiple choice

What does yield do inside a generator function?

  1. Converts the result to a list
  2. Raises StopIteration immediately
  3. Deletes the function
  4. Pauses and produces a valuecorrect

Answer: Pauses and produces a value

Why: yield hands one value to the caller and pauses the function.

02 · Multiple choice

What does the program output?

def gen():
    yield 1
    yield 2
print(list(gen()))
  1. [0, 1]
  2. 1 2
  3. <generator object>
  4. [1, 2]correct

Answer: [1, 2]

Why: list consumes both yielded values and builds a list.

03 · Multiple choice

What does calling a generator function return?

  1. A list of all values
  2. None
  3. A generator objectcorrect
  4. The first yielded value

Answer: A generator object

Why: The body runs only when the generator is advanced.

04 · Multiple choice

What happens when next() reaches the end of a generator?

  1. It restarts the generator
  2. It raises ValueError
  3. It returns None forever
  4. It raises StopIterationcorrect

Answer: It raises StopIteration

Why: StopIteration signals that the generator has no more values.

05 · Fill in the blank

Complete the keyword that yields one value.

def one():
    ____ 1
print(next(one()))

Answer: yield

Why: yield produces a value and pauses the function.

06 · Fill in the blank

Complete the built-in that pulls the next value.

def one():
    yield 1
value = ____(one())
print(value)

Answer: next

Why: next asks a generator for one more value.

07 · Fill in the blank

Complete the function that turns a generator into a list.

def gen():
    yield 1
print(____(gen()))

Answer: list

Why: list consumes the generator and materializes its values.

08 · Fill in the blank

Complete the function that consumes the squares.

print(____(n * n for n in range(4)))

Answer: sum

Why: sum adds every value produced by the generator expression.

09 · Predict output

What appears on the screen?

print(list(n for n in range(3)))

Answer: [0, 1, 2]

Why: The generator expression yields 0, 1, and 2, and list collects them.

10 · Predict output

What is printed?

def g():
    yield "a"
print(next(g()))

Answer: a

Why: next pulls the first yielded string, a.

11 · Predict output

What is the output?

def g():
    yield 1
    yield 2
x = g()
print(next(x), next(x))

Answer: 1 2

Why: The two next calls return one and two, printed on the same line.

12 · Predict output

What does this code print?

def g():
    for n in range(3):
        yield n * 2
print(list(g()))

Answer: [0, 2, 4]

Why: The generator yields 0, 2, and 4, and list shows them.

Try the questions first

Open the practice page for Python Generators and answer the same 12 questions with instant feedback.

Practice this lesson