Comprehensions & Generators · Question 7 · Easy
What does calling next(g) do when g is a generator that still has values left?
- It returns every remaining value as a list
- It restarts the generator from its first yield
- It returns the previous value again without running any code
- It runs the generator up to its next yield and returns that value
Answer and explanation
Answer: D. It runs the generator up to its next yield and returns that value
Explanation: next resumes the generator from where it paused, runs until the next yield and hands back that one value. When nothing is left it raises StopIteration.
Previous: The built-in len is passed to map for a list of city names. What does this… · Next: A conditional expression is used in the output part of a comprehension to mark…
Practise comprehensions & generators
Try a fresh set of comprehensions & generators questions with an explanation after every answer, free and without signing up: Comprehensions & Generators practice. Or sit a full placement mock test.
More comprehensions & generators questions
- A set comprehension collects the first letter of each word. What does len…
- Which of these expressions creates a generator object rather than a fully…
- The built-in len is passed to map for a list of city names. What does this…
- A conditional expression is used in the output part of a comprehension to mark…
- A comprehension has two for clauses, one over range(2) and one over a string…
- The same generator expression is passed to sum twice. What do the two calls…
- One value is taken from a generator function with next before the rest is…
- filter is called with None as its function on a list of mixed values. Which…
- A comprehension walks through a list of rows and keeps only the odd values…
- Kavya has to add up one crore numbers read from a sensor. Why is sum(x for x…
More python practice
Classes & Objects · Exceptions & Files · Built-ins & Standard Library · Variables, Types & Operators · Python Strings · Lists & Tuples · Dictionaries & Sets · Conditions & Loops
LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge