Generators Questions and Answers

Practice Mode
Showing 10 of 25 questions
Q21
Can you reset a generator to its initial state?
  • A No
  • B Yes, using reset()
  • C Yes, using rewind()
  • D Yes, using restart()
Answer: Option A
Explanation: Generators cannot be reset; once exhausted, you need to create a new generator instance.
Q22
What is a common use case for generators?
  • A Processing large files
  • B Mathematical calculations only
  • C GUI programming
  • D Database connections
Answer: Option A
Explanation: Generators are ideal for processing large files or data streams where loading everything into memory is impractical.
Q23
What happens if you try to use a generator after it's exhausted?
  • A Raises StopIteration
  • B Starts from beginning
  • C Returns None
  • D Raises RuntimeError
Answer: Option A
Explanation: Once a generator is exhausted, calling next() on it will always raise StopIteration exception.
Q24
Can generator functions have return statements?
  • A Yes, but it stops the generator
  • B No, syntax error
  • C Only in Python 3.3+
  • D Only without parameters
Answer: Option A
Explanation: Generator functions can have return statements, which will raise StopIteration when encountered during execution.
Q25
What is the performance benefit of generators?
  • A Memory efficiency and pipelining
  • B Faster CPU execution
  • C Better multithreading
  • D Automatic parallelization
Answer: Option A
Explanation: Generators save memory by generating values on-the-fly and allow pipelining of data processing operations.
Questions and Answers for Competitive Exams Various Entrance Test