ProgrammingVery hardGATE CS · Product-company placement

What is printed?

What is printed?
def pipeline():
    nums = (n for n in range(10))
    sq = (n * n for n in nums if n % 2)
    big = (s for s in sq if s > 10)
    return next(big), list(nums)
print(pipeline())
  1. A. (25, [])
  2. B. (25, [5, 6, 7, 8, 9])
  3. C. (9, [4, 5, 6, 7, 8, 9])
  4. D. (25, [6, 7, 8, 9])

Correct answer: D. (25, [6, 7, 8, 9])

Chained generators pull lazily: to get the first value above 10, sq consumes nums up to 5 (squares 1, 9 rejected, 25 accepted). nums is left positioned after 5, so list(nums) gives 6..9.

More Python Programming questions

Exams that ask this topic