ProgrammingVery hardGATE CS · Product-company placement

What is printed?

What is printed?
import itertools
c = itertools.count()
data = ["a", "b", "c"]
pairs = list(zip(c, data))
print(pairs, next(c), list(itertools.compress("abcd", [1, 0, 1, 0])), list(itertools.takewhile(lambda x: x < 3, [1, 2, 5, 1])))
  1. A. [(0, 'a'), (1, 'b'), (2, 'c')] 3 ['a', 'c'] [1, 2]
  2. B. [(0, 'a'), (1, 'b'), (2, 'c')] 4 ['a', 'c'] [1, 2, 1]
  3. C. [(0, 'a'), (1, 'b'), (2, 'c')] 4 ['a', 'c'] [1, 2]
  4. D. [(0, 'a'), (1, 'b'), (2, 'c')] 3 ['b', 'd'] [1, 2, 1]

Correct answer: C. [(0, 'a'), (1, 'b'), (2, 'c')] 4 ['a', 'c'] [1, 2]

zip pulls from count first: on the 4th round it takes 3 from count before finding data exhausted, so that value is lost and next(c) returns 4. takewhile stops for good at the first failing element (5).

More Python Programming questions

Exams that ask this topic