ProgrammingHardInfosys HackWithInfy · TCS CodeVita
What is printed?
What is printed?
class Countdown:
def __init__(self, n):
self.n = n
def __iter__(self):
while self.n > 0:
yield self.n
self.n -= 2
c = Countdown(7)
print(list(c), list(c))- A. [7, 5, 3, 1] [7, 5, 3, 1]
- B. [7, 5, 3, 1] []
- C. [7, 5, 3] []
- D. [7, 5, 3, 1] [1]
Correct answer: B. [7, 5, 3, 1] []
The loop mutates self.n, so after the first list the object is exhausted. A second iteration starts from n = -1 and yields nothing.
More Python Programming questions
- What is printed? python from itertools import permutations p = sorted(permutations('cab')) print([''.join(x) for x in p][3], len(p))
- What is printed? python print(() is (), [] is [], (1, 2) == (1, 2))
- A program must answer 10^5 range-sum queries over an array of 10^5 numbers in CPython. Which approach is fastest?
- What is printed? python from functools import lrucache class C: @lrucache(maxsize=None) def f(self, x): return x 2 a, b = C(), C() a.f(1)…
- What is printed? python class A: def init(self, v): self.v = v def eq(self, o): return isinstance(o, A) and self.v == o.v xs = [A(1)…
- What is printed? python a, b = 5, 9 a ^= b b ^= a a ^= b print(a, b)