ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
class Count:
total = 0
def __init__(self):
self.total += 1
Count.total += 10
a = Count(); b = Count()
print(a.total, b.total, Count.total)- A. 1 1 20
- B. 1 11 20
- C. 11 21 20
- D. 2 2 20
Correct answer: B. 1 11 20
self.total += 1 reads the class value (0 for a, then 10 for b) and creates an instance attribute; Count.total += 10 updates the class. So a.total = 0+1, b.total = 10+1, class total 20.
More Python Programming questions
- What is printed? python class Base: def initsubclass(cls, prefix="x", kw): super().initsubclass(kw) cls.tag = prefix + cls.name class…
- What is printed? python class It: def init(self): self.i = 0 def iter(self): return self def next(self): self.i += 1 if self.i > 3: raise…
- What is printed? python class Ctx: def init(self, n): self.n = n def enter(self): print("in", self.n, end=" ") return self.n 10 def…
- What is printed? python from contextlib import contextmanager @contextmanager def cm(): print("A", end=" ") try: yield "B" except…
- What is printed? python from itertools import accumulate, chain, islice, count print(list(accumulate([1, 2, 3, 4])), list(chain("ab"…
- What is printed? python from itertools import ziplongest, product, combinations, permutations print(list(ziplongest("ab", [1, 2, 3]…