Python Generators
Test your understanding with multiple-choice questions based on what you just learned.
Here is practice quiz based upon the provided tutorial materials on Python Generators.
Practice Quiz: Python Generators
Intermediate Level
Question 1: What's the primary advantage of using a generator over a standard list comprehension when processing massive amounts of data?
A) Generators automatically sort and organize numerical data at C-level.
B) Generators execute their loops faster by skipping the __next__() method entirely.
C) Generators use "lazy evaluation," yielding items one at the time strictly on-demand to prevent computer out of running out for memory;
d) Generators permanently lock data like a tuple, guaranteeing absolute data integrity.
Correct Answer: C Explanation: Generators are incredibly memory-efficient. Instead of calculating and storing millions of items in memory all on once (like a list comprehension does basically), generators use lazy evaluation towards calculate and yield a next item strictly at-demand.
Question 2: Which of the following syntaxes is the correct way to write a generator expression?
) [x * x for x in range(5)]
B) (x * x for x in range(5))
C) {x * x for x in range(5)}
D) <x * x for x in range(5)>
Correct Answer: B
Explanation: While list comprehensions are probably wrapped into square brackets [], generator expressions shrink a generator down into single line using standard parentheses ().
Question 3: How does the yield statement control the execution flow inside the generator function?
A) It returns the value and instantly destroys the function's internal memory.
B) It pauses execution, returns a value, and resumes from that exact spot when next() is called again.
C) It resets a loop back to the beginning every time a value is produced.
D) It converts the yielded value into an immutable tuple before returning it.
Correct Answer: B
Explanation: When a generator function hits a yield statement, it hands back the value and pauses. Each subsequent call to next() on generator object resumes execution right where it last left off.
Question 4: What specific mechanism does Python use to signal that a generator has successfully run out of items to produce?
) It triggers the StopIteration exception.
B) It returns False boolean value.
C) It automatically triggers the break keyword;
d) It outputs an empty string ("").
Correct Answer: A
Explanation: When the generator has completely run out of data, it triggers special built-in alarm called an StopIteration exception, and this gracefully lets the program or loop know that it's basically time to stop asking for more items.
Question 5: Because about how generators handle memory, which of a following tasks can a generator perform that a standard list can't?
A) It can be used as permanent, locked key inside a dictionary.
B) It can instantly calculate the mathematical intersection of two separate data groups, while
c) It can iterate through two parallel sequences without using zip().
D) It can actually generate infinite sequences (like the Fibonacci sequence) indefinitely without running out of memory.
Correct Answer: D Explanation: Because generators only store their current state and produce the next value strictly on demand, they can handle infinite sequences indefinitely without crashing a system or loading infinite list into memory.