ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
import itertools a, b = itertools.tee(iter([1, 2, 3])) next(a); next(a) print(list(b), list(a))
- A. [3] [3]
- B. [1, 2, 3] [1, 2, 3]
- C. [] [3]
- D. [1, 2, 3] [3]
Correct answer: D. [1, 2, 3] [3]
tee gives two independent iterators over the same source, buffering values one of them has not consumed yet; advancing a twice does not affect b.
More Python Programming questions
- What is printed? python def nums(): for n in range(5): print(n, end="") yield n r = any(n > 1 for n in nums()) print("", r)
- What is printed? python gen = (i for i in range(3)) print(1 in gen, 1 in gen, list(gen))
- What is printed? python x = 10 class K: x = 20 y = [x + i for i in range(2)] z = (lambda: x)() print(K.y, K.z)
- What is printed? python class K: n = 3 a = [i for i in range(n)] try: b = [n for i in range(2)] except NameError: b = "NE" print(K.a, K.b)
- What is printed? python total = 0 squares = [total := total + v for v in [1, 2, 3]] print(squares, total)
- What is printed? python print([[r c for c in range(r)] for r in range(1, 4)])