Loading

Quipoin Menu

Learn • Practice • Grow

python / Iterators and Generators
mcq
Direction: Choose the correct option

Q1.

What is an iterator in Python?
A. An object that implements __iter__ and __next__
B. A dictionary
C. A function with yield
D. A list
Direction: Choose the correct option

Q2.

What does the `iter()` function do?
A. Converts to list
B. Creates a generator
C. Returns the next item
D. Returns an iterator object from an iterable
Direction: Choose the correct option

Q3.

What is a generator in Python?
A. A function that yields values with `yield`
B. An iterator created by `iter()`
C. A function that returns a list
D. A class with `__iter__`
Direction: Choose the correct option

Q4.

What does `yield` do in a function?
A. Raises an error
B. Returns multiple values
C. Exits the function
D. Pauses the function and returns a value
Direction: Choose the correct option

Q5.

How do you iterate over a generator?
A. Only `for` loop
B. Cannot iterate
C. Use `for` loop or `next()`
D. Only `next()`
Direction: Choose the correct option

Q6.

What is the output of `def gen(): yield 1; yield 2; list(gen())`?
A. [1,2]
B. [1,2]
C. [1,2]
D. [1,2]
Direction: Choose the correct option

Q7.

What is the difference between a list comprehension and a generator expression?
A. Generator expression is faster
B. List comprehension uses parentheses
C. List comprehension creates a list; generator expression creates a generator
D. They are the same
Direction: Choose the correct option

Q8.

What happens when a generator is exhausted?
A. Returns empty
B. Resets
C. Raises StopIteration
D. Returns None
Direction: Choose the correct option

Q9.

Can a generator be used only once?
A. No, it resets
B. No, it can be reused
C. Yes, it's exhausted after one iteration
D. Yes, but can be recreated
Direction: Choose the correct option

Q10.

What is the purpose of `yield from`?
A. Yields with yield
B. Yields from a function
C. Yields multiple values
D. Delegates to another generator or iterable