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)
  1. A. 7
  2. B. 700
  3. C. 0
  4. 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

Exams that ask this topic