ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
class C:
n = 0
def __init__(self):
C.n += 1
@classmethod
def total(cls):
return cls.n
class D(C):
pass
D(); C(); D()
print(C.total(), D.total())- A. 3 3
- B. 1 3
- C. 1 2
- D. 3 2
Correct answer: A. 3 3
C.n += 1 always updates the attribute on C itself, even when a D object is created. D has no n of its own, so cls.n for D finds C.n by inheritance. Both totals are 3.
More Python Programming questions
- What is printed? python e = 5 try: 1 / 0 except ZeroDivisionError as e: pass try: print(e) except NameError: print("NameError")
- How many of the following statements about Python are correct? (i) A tuple that contains a list can be used as a dictionary key. (ii) A…
- What is printed? python a = [1, 2, 3] a += [4] a = a + [5] a.extend("67") print(a)
- What is printed? python x = [[0] 3] 2 y = [[0] 3 for in range(2)] x[0][1] = 5 y[0][1] = 5 print(x, y)
- What is printed? python def add(item, bucket=[]): bucket.append(item) return bucket a = add(1) b = add(2) c = add(3, []) print(a, b, c, a…
- What is printed? python fs = [lambda: i i for i in range(4)] gs = [lambda i=i: i i for i in range(4)] print([f() for f in fs], [g() for g…