How do you manually create an iterator in Python?
A. Using a generator function
B. Using a list comprehension
C. Using a for loop
D. Using a class with __next__() method
Answer: Option D
Solution (By Examveda Team)
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.
What is a generator in Python?
A. A function that generates random numbers
B. A special type of list
C. A way to define classes
D. A function that yields values one at a time
How is a generator different from a regular function?
A. A generator uses the return keyword
B. A generator can yield multiple values
C. A generator uses the break statement
D. A generator uses the continue statement
What is an advantage of using generators for large datasets?
A. They use more memory
B. They are slower than loops
C. They are easier to implement
D. They use less memory
How do you define a generator function in Python?
A. Using the generator keyword
B. Using the def keyword and yield statement
C. Using the gen keyword
D. Using the function keyword

Join The Discussion