Python Generators and Iterators MCQ Questions and Answers

1. What is a generator in Python?

a) A tool to generate new Python versions
b) A function that returns an iterator
c) A type of collection like lists and tuples
d) A built-in Python module

2. How do you define a generator function in Python?

a) A function that uses the generate keyword
b) A function that returns values using the yield keyword
c) A function that includes an iterator expression
d) A function that is declared with the generator keyword

3. What does the 'yield' keyword do in a generator function?

a) Stops the function permanently
b) Returns a value and continues from this point on the next call
c) Breaks the function execution
d) Yields control to another function

4. What is an iterator in Python?

a) A built-in function for iteration
b) An object that can be iterated over
c) A type of data structure
d) A syntax for looping

5. How do you manually iterate through an iterator in Python?

a) Using a for loop
b) Using the next() function
c) By indexing
d) Using the iter() function

6. What is the output of calling next() on an exhausted iterator?

a) An empty value
b) The first value of the iterator
c) A StopIteration exception
d) None

7. How do you convert a list into an iterator?

a) Using the iter(list) function
b) Using the iterator(list) function
c) By indexing the list
d) All lists are automatically iterators

8. What is the difference between an iterator and a generator in Python?

a) An iterator is a built-in function, and a generator is not
b) An iterator can't be paused, but a generator can
c) An iterator is used for iteration, and a generator for generation
d) There is no difference

9. How do you create a generator expression?

a) Using square brackets []
b) Using parentheses ()
c) Using curly braces {}
d) Using the generator keyword

10. How is a generator different from a list comprehension in Python?

a) A generator is faster than a list comprehension
b) A generator does not create the entire sequence at once
c) A generator can only be used once
d) b and c

11. What is the 'yield from' statement used for in Python generators?

a) To yield from another generator
b) To yield all values at once
c) To exit the generator
d) To yield from a list

12. Can a generator function have more than one 'yield' statement?

a) Yes
b) No
c) Only in special cases
d) Only with a 'yield from' statement

13. How do you get a list of all values from a generator object?

a) Using the list(generator) function
b) By iterating with a for loop
c) Using the values() method of the generator
d) Both a and b

14. What is the main advantage of using generators in Python?

a) Improved performance and lower memory usage
b) More concise code
c) Easier error handling
d) Faster execution speed

15. What does the __iter__ method do in a Python class to make it an iterator?

a) Returns the next item
b) Initializes the iteration
c) Returns the iterator object itself
d) Starts the iteration from the beginning

Comments