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'))- A. [0, 1, 4, 9] 14 0
- B. [0, 1, 4, 9] 14 []
- C. [0, 1, 4] 5 none
- 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
- What is printed? python x = 255 print(f'{x:x}', f'{x:08b}', f'{x:o}', f'{x:#06x}')
- What is printed? python print(3 in {1: 'a', 3: 'b'}, '3' in {1: 'a', 3: 'b'}, 3 in [1, 3], 3 in range(3, 4))
- What is printed? python print(list(zip('ab', 'cd', 'ef'))[1], dict(zip('ab', [1])))
- What is printed? python print(len('héllo'), len('héllo'.encode('utf-8')), 'é'.encode('utf-8'))
- What is printed? python import math print(math.floor(7 / 2) 2 + 7 % 2, 7 // 2 2 + 7 % 2)
- What is printed? python x = [0] 3 x[0] += 1 x = x 2 x[3] += 5 print(x)