ProgrammingVery hardGATE CS · Product-company placement
What is printed?
What is printed?
def g():
total = 0
while True:
x = yield total
if x is None:
return total * 100
total += x
it = g()
next(it)
it.send(3); it.send(4)
try:
next(it)
except StopIteration as e:
print(e.value)- A. 7
- B. 700
- C. 0
- D. None
Correct answer: B. 700
After sending 3 and 4, total is 7. next(it) sends None, so the generator returns 700; a return value in a generator travels in StopIteration.value.
More Python Programming questions
- What is printed? python def inner(): a = yield "x" b = yield "y" return (a, b) def outer(): r = yield from inner() yield r o = outer()…
- What is printed? python def g(): try: yield 1 yield 2 except KeyError: yield "caught" yield 3 it = g() print(next(it), it.throw(KeyError)…
- What is printed? python def g(): yield 1 raise StopIteration try: print(list(g())) except RuntimeError as e: print("RuntimeError")
- What is printed? python def g(): try: yield 1 finally: print("F", end=" ") it = g() next(it) it.close() print(next(it, "done"))
- What is printed? python import itertools a, b = itertools.tee(iter([1, 2, 3])) next(a); next(a) print(list(b), list(a))
- 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)