Generators Questions and Answers
Python Programming - Generators Questions with Answers focus on efficient data iteration using the `yield` keyword. These Python Generators Questions are helpful for TCS, Infosys, and Capgemini exams to test knowledge of memory-efficient programming techniques
Generators
Showing 10 of
25 questions
1. What is a generator in Python?
- A function that returns an iterable generator object
- A special type of list that stores data
- A Python module for mathematical operations
- A built-in Python data type
2. Which keyword is used to create a generator function?
- yield
- return
- generate
- generator
3. What is the main advantage of using generators over lists?
- Memory efficiency
- Faster execution for all operations
- Easier to debug
- Better error handling
4. How do you create a generator expression?
- Using parentheses ()
- Using square brackets []
- Using curly braces {}
- Using angle brackets <>
5. What happens when a generator function calls yield?
- Function pauses and returns value
- Function terminates immediately
- Function starts from beginning
- Function raises an exception
6. Which method is used to get the next value from a generator?
- next()
- get()
- yield()
- iterate()
7. What exception is raised when a generator is exhausted?
- StopIteration
- GeneratorEmpty
- NoMoreValues
- EndOfGenerator
8. Can a generator function have multiple yield statements?
- Yes
- No
- Only in Python 3
- Only with special decorators
9. What is the output of: (x*x for x in range(3))
- A generator object
- [0, 1, 4]
- (0, 1, 4)
- Error
10. How can you iterate through all values of a generator?
- Using a for loop
- Using while True loop
- Calling next() repeatedly
- All of the above