ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
def gen_squares(n):
    for i in range(n):
        yield i * i
print(list(gen_squares(4)), sum(gen_squares(4)), max(gen_squares(0), default='none'))
  1. A. [0, 1, 4, 9] 14 0
  2. B. [0, 1, 4, 9] 14 []
  3. C. [0, 1, 4] 5 none
  4. D. [0, 1, 4, 9] 14 none

Correct answer: D. [0, 1, 4, 9] 14 none

The generator yields 0, 1, 4, 9 for n = 4 and sums to 14. For n = 0 the generator is empty, so max falls back to the default 'none'.

More Python Programming questions

Exams that ask this topic