Python generators simplify memory-efficient iteration using the yield keyword. This quiz helps you master Python’s generator functions, expressions, lazy evaluation, and internal working with practical examples.
Time limit: 0
Quiz Summary
0 of 15 Questions completed
Questions:
Information
You have already completed the quiz before. Hence you can not start it again.
Which keyword is used to define a generator function?
Correct
Incorrect
Question 3 of 15
3. Question
What is the output of this code?
def gen(): yield 1 yield 2
g = gen() print(next(g))
Correct
Incorrect
Question 4 of 15
4. Question
What will happen if you keep calling next() on an exhausted generator?
Correct
Incorrect
Question 5 of 15
5. Question
What type does a generator return?
Correct
Incorrect
Question 6 of 15
6. Question
Which of the following is not true about generators?
Correct
Incorrect
Question 7 of 15
7. Question
What is the use of the yield keyword?
Correct
Incorrect
Question 8 of 15
8. Question
Which of the following is a valid use case for generators?
Correct
Incorrect
Question 9 of 15
9. Question
What is the output of this code?
def count(): n = 1 while True: yield n n += 1
c = count() for _ in range(3): print(next(c))
Correct
Incorrect
Question 10 of 15
10. Question
Can a generator function contain the return statement?
Correct
Incorrect
Question 11 of 15
11. Question
Which method is automatically called when using next() on a generator?
Correct
Incorrect
Question 12 of 15
12. Question
What will this code output?
def squares(): for i in range(3): yield i * i
print(list(squares()))
Correct
Incorrect
Question 13 of 15
13. Question
What is a generator expression?
Correct
Incorrect
Question 14 of 15
14. Question
What is the output of this generator expression?
gen = (x*2 for x in range(3)) print(next(gen))
Correct
Incorrect
Question 15 of 15
15. Question
Which of the following is more memory-efficient for large sequences?
Correct
Incorrect
Summary
Python generators enable powerful lazy evaluation for large or infinite data streams using the yield keyword. This quiz equips you with the practical and theoretical understanding of how generators work, where to use them, and how to write them efficiently.
At QuizOrbit, our team specializes in fun, informative quizzes across topics like general knowledge, technology, and entertainment. Boost your knowledge with our expert-designed quizzes.