ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
def counter_gen():
c = 0
while True:
cmd = yield c
if cmd == "reset":
c = 0
else:
c += 1
g = counter_gen()
print(next(g), next(g), next(g), g.send("reset"), next(g))- A. 0 1 2 0 1
- B. 0 1 2 3 0
- C. 0 1 2 0 0
- D. 1 2 3 0 1
Correct answer: A. 0 1 2 0 1
The first next starts the generator and yields 0. Each later next sends None, so c increments. send('reset') sets c to 0 and loops to yield 0; the following next increments to 1.
More Python Programming questions
- What is printed? python import itertools c = itertools.count() data = ["a", "b", "c"] pairs = list(zip(c, data)) print(pairs, next(c)…
- What is printed? (Output lines are shown separated by " / ".) python def f(x): return x 2 g = f del f print(g(3), g.name) try: f(1) except…
- What is printed? python x = [1, 2, 3] it = iter(x) next(it) x.insert(0, 0) print(next(it), list(it))
- What is printed? python words = ["apple", "Banana", "cherry", "date"] bylen = {} for w in words: bylen.setdefault(len(w), []).append(w)…
- What is printed? python def outer(): results = [] def rec(n): if n == 0: results.append("x") return for in range(n): rec(n - 1) rec(3)…
- What is printed? python a = "abc" b = "".join(["a", "b", "c"]) print(a == b, a is b or True, sorted(set(a + b)), a.find("c") ==…