ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
def evens():
n = 0
while True:
yield n
n += 2
def take(k, it):
return [next(it) for _ in range(k)]
e = evens()
print(take(3, e), take(2, e), take(1, evens()))- A. [0, 2, 4] [0, 2] [0]
- B. [0, 2, 4] [6, 8] [10]
- C. [0, 2, 4] [6, 8] [0]
- D. [0, 2, 4] [4, 6] [0]
Correct answer: C. [0, 2, 4] [6, 8] [0]
The same generator object e keeps its state between take calls, so the second call continues from 6. A new evens() starts again at 0.
More Python Programming questions
- What is printed? python 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)…
- What is printed? python def g(): print("a", end=" ") x = yield print("b", x, end=" ") yield x 2 it = g() print(it.send(None), end=" ")…
- What is printed? python def g(): try: yield 1 except GeneratorExit: print("exit", end=" ") yield 2 it = g() next(it) try: it.close()…
- What is printed? (Output lines are shown separated by " / ".) python it = iter([1, 2, 3, 4, 5, 6]) print([(a, b) for a, b in zip(it, it)]…
- What is printed? python s = {1, 2, 3} t = {3, 4} print(s | t, s & t, s - t, s ^ t, s >= {1, 2})
- What is printed? python a = {1, 2} b = a a |= {3} c = frozenset(a) a = a | {4} print(b, c, a, type(c | {9}).name)