ProgrammingVery hardInfosys HackWithInfy · TCS CodeVita
What is printed?
What is printed?
def acc():
total = 0
while True:
x = yield total
if x is None:
return total
total += x
g = acc()
next(g)
g.send(5)
g.send(7)
try:
g.send(None)
except StopIteration as e:
print(e.value)- A. 5
- B. 7
- C. 12
- D. 0
Correct answer: C. 12
Each send resumes the yield with the sent value and yields the new total. Sending None makes the generator return 12, which is stored in StopIteration.value.
More Python Programming questions
- What is printed? python class P: def init(self, x): self.x = x def eq(self, o): return self.x == o.x try: s = {P(1)} except TypeError…
- What is printed? python class S: slots = ('a',) s = S() s.a = 1 try: s.b = 2 except AttributeError: print('no dict', s.a)
- What is printed? python print(len(set(map(lambda x: x % 3, range(10)))), sum(1 for in filter(None, [0, 1, '', 'a', None])))
- What is printed? python def mpow(b, e, m): r = 1 b %= m while e: if e & 1: r = r b % m b = b b % m e >>= 1 return r print(mpow(2, 10…
- What is printed? python import math n = 1016 - 1 print(math.isqrt(n), int(n 0.5))
- What is printed? python print(sorted([('b', 2), ('a', 2), ('c', 3)], key=lambda t: (-t[1], t[0])))