ExamVeda
Login
Home
11
How is a generator expression defined in Python?
Discuss
Answer & Solution
Answer: Option B
Solution:
The correct answer is Option B: Using parentheses (...).
A generator expression in Python is defined using parentheses (...) syntax. Inside the parentheses, you write an expression that generates values, similar to a list comprehension but with parentheses instead of square brackets. The expression can be a simple expression or a more complex one involving conditionals or nested expressions. When iterated over, a generator expression produces values lazily, one at a time, without storing the entire sequence in memory at once. This lazy evaluation mechanism makes generator expressions efficient for handling large datasets or infinite sequences.
12
What is the primary purpose of the itertools module in Python?
Discuss
Answer & Solution
Answer: Option C
Solution:
The correct answer is Option C: To work with iterators and generators.
The primary purpose of the itertools module in Python is to work with iterators and generators. This module provides a collection of functions for creating and manipulating iterators for efficient looping. It offers various iterator-building blocks that can be combined to form complex iterators or generators. Some commonly used functions in itertools include count(), cycle(), repeat(), chain(), zip_longest(), and many more. These functions enable developers to write efficient and concise code when working with iterators and generators, making itertools an essential tool for tasks involving iteration and data processing.
13
What is an iterator in Python?
Discuss
Answer & Solution
Answer: Option C
Solution:
The correct answer is Option C: An object that produces values one at a time.
An iterator in Python is an object that produces values one at a time. It implements two methods, iter() and next(), which enable it to be iterated over using a for loop or by calling the next() function. When iterating over an iterator, it produces values lazily, generating the next value only when requested. This lazy evaluation mechanism allows iterators to efficiently handle large datasets or infinite sequences without loading all values into memory at once.
14
Which built-in Python function is used to create an iterator from an iterable?
Discuss
Answer & Solution
Answer: Option B
Solution:
The correct answer is Option B: iter().
The iter() function is used to create an iterator from an iterable object in Python. When called with an iterable object as its argument, such as a list, tuple, or string, iter() returns an iterator object that can be iterated over using a for loop or by calling the next() function. This allows for easy iteration over the elements of the iterable, one at a time, using the iterator protocol.
15
What is the role of the __iter__() and __next__() methods in iterators?
Discuss
Answer & Solution
Answer: Option C
Solution:
The correct answer is Option C: They define iteration behavior.
The iter() and next() methods in iterators are used to define the iteration behavior of an object.

iter() method returns the iterator object itself and is called when the iterator is initialized. It sets up the iterator for iteration.
next() method returns the next item in the sequence and is called each time the next item is requested during iteration. It raises a StopIteration exception when there are no more items to return.

These methods together define the iterator protocol, allowing objects to be iterated over using a for loop or by calling the next() function.
16
What is the purpose of using the yield from statement in generators?
Discuss
Answer & Solution
Answer: Option B
Solution:
The correct answer is Option B: To yield values from another generator.
The yield from statement in generators is used to delegate the generation of values to another generator. When a generator encounters yield from, it delegates the generation of values to another iterable (which could be another generator), and yields values from it one by one. This simplifies the syntax for delegating to sub-generators, making the code more readable and concise. Additionally, it allows for the propagation of exceptions raised by the sub-generator back to the main generator.
17
What is the advantage of using generator expressions over list comprehensions?
Discuss
Answer & Solution
Answer: Option B
Solution:
The correct answer is Option B: List comprehensions consume less memory.
List comprehensions and generator expressions both offer a concise syntax for creating sequences in Python. However, one advantage of using generator expressions over list comprehensions is that generator expressions consume less memory. While list comprehensions eagerly generate all values and store them in memory as a list, generator expressions produce values lazily, one at a time, as they are requested. This lazy evaluation mechanism allows generator expressions to save memory, especially when dealing with large datasets or infinite sequences. Therefore, generator expressions are more memory-efficient compared to list comprehensions.
18
How do you manually create an iterator in Python?
Discuss
Answer & Solution
Answer: Option D
Solution:
The correct answer is Option D: Using a class with next() method.
To manually create an iterator in Python, you can define a class that implements the iterator protocol by including the iter() and next() methods. The iter() method returns the iterator object itself, and the next() method returns the next item in the sequence or raises a StopIteration exception when there are no more items to return. By using this approach, you have full control over the iteration behavior and can define custom logic for generating the sequence of values.
19
What is the purpose of the itertools.chain() function?
Discuss
Answer & Solution
Answer: Option B
Solution:
The correct answer is Option B: To merge multiple iterators into one.
The purpose of the itertools.chain() function is to merge multiple iterators into one single iterator. When called with multiple iterable arguments, chain() returns an iterator that yields values from each iterable consecutively, effectively chaining them together into a single sequence. This is useful when you need to iterate over multiple sequences as if they were one continuous sequence, without creating a new combined data structure like a list.
20
How can you create an infinite generator in Python?
Discuss
Answer & Solution
Answer: Option C
Solution:
The correct answer is Option C: By using the while loop.
You can create an infinite generator in Python by using a while loop. Inside the while loop, you can use the yield statement to yield values infinitely. Since a while loop can continue indefinitely as long as its condition remains true, you can use it to create a generator that produces an infinite sequence of values. This allows you to generate values lazily, one at a time, without having to predefine the length of the sequence.