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

Answer:

b) A function that returns an iterator

Explanation:

A generator in Python is a function that returns an iterator, which we can iterate over one value at a time.

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

Answer:

b) A function that returns values using the yield keyword

Explanation:

Generator functions use the yield statement to return data during the function execution.

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

Answer:

b) Returns a value and continues from this point on the next call

Explanation:

The 'yield' statement returns a value and pauses the function execution, which can be resumed later.

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

Answer:

b) An object that can be iterated over

Explanation:

An iterator is an object that implements the iterator protocol (has __next__() and __iter__() methods).

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

Answer:

b) Using the next() function

Explanation:

The next() function retrieves the next item from an iterator.

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

Answer:

c) A StopIteration exception

Explanation:

Once an iterator is exhausted, further calls to next() raise a StopIteration exception.

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

Answer:

a) Using the iter(list) function

Explanation:

The iter() function converts an iterable into an iterator.

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

Answer:

b) An iterator can't be paused, but a generator can

Explanation:

Generators can pause execution and maintain state, while iterators cannot.

9. How do you create a generator expression?

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

Answer:

b) Using parentheses ()

Explanation:

Generator expressions are similar to list comprehensions but are created with parentheses instead of square brackets.

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

Answer:

d) b and c

Explanation:

Generators generate values on the fly and do not store the entire sequence in memory. They also can only be iterated over once.

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

Answer:

a) To yield from another generator

Explanation:

The 'yield from' statement is used for yielding all values from another generator or iterable.

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

Answer:

a) Yes

Explanation:

Generator functions can have multiple 'yield' statements to yield multiple times during their execution.

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

Answer:

d) Both a and b

Explanation:

You can convert a generator object into a list using the list() function or by iterating over it with a for loop.

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

Answer:

a) Improved performance and lower memory usage

Explanation:

Generators provide performance benefits by consuming less memory and allow for the representation of sequences that would be impractical to store in memory.

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

Answer:

c) Returns the iterator object itself

Explanation:

The __iter__ method should return the iterator object itself, and is required to make an object iterable.


Comments