Generators Questions and Answers

Practice Mode
Showing 10 of 25 questions
Q11
What is the difference between return and yield?
  • A return ends function, yield pauses it
  • B yield ends function, return pauses it
  • C Both work the same way
  • D return is for generators, yield for normal functions
Answer: Option A
Explanation: return terminates the function completely, while yield pauses the function and remembers its state for next call.
Q12
Can generators be used with infinite sequences?
  • A Yes
  • B No
  • C Only with special imports
  • D Only in Python 2
Answer: Option A
Explanation: Generators are ideal for infinite sequences as they generate values on demand without storing them all in memory.
Q13
How do you create a generator that yields numbers from 0 to n?
  • A def gen(n): for i in range(n): yield i
  • B def gen(n): return range(n)
  • C def gen(n): yield range(n)
  • D def gen(n): return [i for i in range(n)]
Answer: Option A
Explanation: Generator functions use yield to produce values one at a time within a loop.
Q14
What is generator comprehension?
  • A Creating generators with concise syntax
  • B Reading generator documentation
  • C Optimizing generator performance
  • D Debugging generator functions
Answer: Option A
Explanation: Generator comprehension uses parentheses and creates generator objects similar to list comprehensions but lazily.
Q15
Can you send values back into a generator?
  • A Yes, using send() method
  • B No, generators are read-only
  • C Only in Python 2
  • D Only with special decorators
Answer: Option A
Explanation: Generators can receive values using the send() method, allowing two-way communication between caller and generator.
Q16
What does the close() method do on a generator?
  • A Raises GeneratorExit in generator
  • B Deletes the generator from memory
  • C Resets the generator to start
  • D Continues execution till end
Answer: Option A
Explanation: The close() method raises GeneratorExit exception at the point where the generator was paused, terminating it.
Q17
What is the purpose of yield from in Python?
  • A Delegate to another generator
  • B Yield from a specific index
  • C Create a new generator
  • D Yield all values at once
Answer: Option A
Explanation: yield from is used to delegate to another generator, simplifying code when working with nested generators.
Q18
Can generators be pickled in Python?
  • A No
  • B Yes
  • C Only in Python 2
  • D Only with simple generators
Answer: Option A
Explanation: Generators cannot be pickled because they contain execution state that cannot be serialized.
Q19
What is the state of a generator between yield calls?
  • A Suspended
  • B Running
  • C Terminated
  • D Reset
Answer: Option A
Explanation: Generators maintain their local variables and execution position between yield calls.
Q20
How are generators different from iterator objects?
  • A Generators use yield, iterators use __next__
  • B Iterators are faster
  • C Generators are not iterators
  • D No difference
Answer: Option A
Explanation: Generators are a convenient way to create iterators using functions with yield, while iterators require implementing __iter__ and __next__ methods.
Questions and Answers for Competitive Exams Various Entrance Test