ProgrammingModerateProduct-company MCQ pattern
What is printed?
What is printed?
class A:
count = 0
def __init__(self):
A.count += 1
self.count = 10
a = A(); b = A()
b.count += 5
print(A.count, a.count, b.count)- A. 12 10 15
- B. 2 2 7
- C. 2 10 15
- D. 2 10 10
Correct answer: C. 2 10 15
A.count is the class attribute, incremented once per object (2). self.count = 10 creates an instance attribute that shadows it on each object; b.count += 5 changes only b's own attribute.
More Python Programming questions
- What is printed? python it = iter([1, 2, 3, 4, 5]) pairs = list(zip(it, it)) print(pairs, list(it))
- What is printed? python import copy a = [[1, 2], [3]] b = copy.copy(a) c = copy.deepcopy(a) a[0].append(9) a.append([4]) print(b, c)
- What is printed? python print(bool([]), bool([0]), bool(""), bool(" "), None == False, 0 == False)
- What is printed? python def countcalls(fn): def wrapper(a): wrapper.calls += 1 return fn(a) wrapper.calls = 0 return wrapper @countcalls…
- What is printed? python def make(): acc = [] for i in range(3): def f(x, i=i): return x i acc.append(f) acc.append(lambda x: x + i) return…
- What is printed? python class A: def go(self): return ["A"] class B(A): def go(self): return ["B"] + super().go() class C(A): def…