Python Programming - Generators
Exercise : Generators - General Questions
- Generators - General Questions
31.
How does the
generator.__iter__() method differ from the iter() built-in function when applied to a generator?
Answer: Option
Explanation:
generator.__iter__() returns the generator itself, treating the generator as its own iterator. iter() returns a new iterator object for the generator.
32.
What is the purpose of the
itertools.islice() function when used with generators?
Answer: Option
Explanation:
itertools.islice() is used to slice a generator into specified ranges, allowing you to extract a portion of the generator's elements without consuming the entire sequence.
33.
How can you implement a generator that produces a sequence of powers of 2?
Answer: Option
Explanation:
A generator for a sequence of powers of 2 can be implemented using a loop with a
yield statement.
34.
How does the
generator.send(value) method differ from next(generator) when used with a generator?
Answer: Option
Explanation:
next(generator) can be used without providing a value, while generator.send(value) requires a value to be sent into the generator.
35.
What does the
itertools.permutations() function do when used with a generator?
Answer: Option
Explanation:
itertools.permutations() generates all possible permutations of the elements in the generator.
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers