Comprehensions & Generators · Question 16 · Hard

The generator function below contains print calls around its yield. In what order do the letters appear?

def numbers():
    print("A")
    yield 1
    print("B")

g = numbers()
print("C")
next(g)
print("D")
  1. A C D
  2. A C B D
  3. C A B D
  4. C A D
Answer and explanation

Answer: D. C A D

Explanation: Calling numbers() only creates the generator; none of its body runs yet, so C is printed first. next(g) runs the body up to the yield, printing A, and B is never reached because the generator is not resumed again.

Previous: A dictionary comprehension swaps keys and values of a price list in which two… · Next: A list comprehension uses a loop variable with the same name as a module-level…

Practise comprehensions & generators

Try a fresh set of comprehensions & generators questions with an explanation after every answer, free and without signing up: Comprehensions & Generators practice. Or sit a full placement mock test.

More comprehensions & generators questions

More python practice

Classes & Objects · Exceptions & Files · Built-ins & Standard Library · Variables, Types & Operators · Python Strings · Lists & Tuples · Dictionaries & Sets · Conditions & Loops

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge