ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
def gen():
yield 1
return 5
yield 2
it = gen()
print(list(it), next(it, 'done'))- A. [1] 5
- B. [1, 5] done
- C. [1] done
- D. [1, 2] done
Correct answer: C. [1] done
list(it) consumes the generator until the return statement, which ends it without yielding 5, so the list is [1]. The generator is then exhausted, and next(it, 'done') gives the default.
More Python Programming questions
- What is printed? python from itertools import accumulate print(list(accumulate([2, 3, 4], lambda a, b: a b)), list(accumulate([1, -1, 2])))
- What is printed? python a = [[1, 2], [3, 4]] b = [row[:] for row in a] c = a[:] a[0][0] = 9 c[1][1] = 7 print(b, c)
- What is printed? python print(len(str(2 64)), (2 64).bitlength(), 2 64 % 9, pow(2, 64, 9))
- Which technique gives the lowest total cost for answering q range-sum queries on a fixed Python list of length n, where n and q are both 2…
- What is printed? python x = 7 print(x // 2 if x % 2 else x / 2, x > 3 > 2, not x > 3 == 4)
- What is printed? python print(pow(-2, 3, 5), pow(3, -1, 7), pow(2, 10, -7), (-2) 3 % 5)