ProgrammingVery hardGATE CS · Product-company placement

What is printed?

What is printed?
def nums():
    for n in range(5):
        print(n, end="")
        yield n
r = any(n > 1 for n in nums())
print("", r)
  1. A. 01234 True
  2. B. 012 True
  3. C. 2 True
  4. D. 012 2

Correct answer: B. 012 True

any() short-circuits: it pulls items only until one is truthy (n = 2). The generator prints 0, 1 and 2 and is never advanced further.

More Python Programming questions

Exams that ask this topic