ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
def gen():
    x = yield 1
    print('got', x)
    yield 2
g = gen()
print(next(g))
print(g.send('hi'))
  1. A. 1 got hi 2
  2. B. 1 2
  3. C. 1 got None 2
  4. D. 1 hi 2

Correct answer: A. 1 got hi 2

The first next() runs to the first yield and returns 1. send('hi') resumes the generator, so the yield expression evaluates to 'hi', which is printed before the next yield returns 2.

More Python Programming questions

Exams that ask this topic