ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
class A:
    n = []
    m = 0
a, b = A(), A()
a.n.append(1)
a.m += 1
b.n = [2]
print(A.n, A.m, a.n, b.n, a.m, b.m)
  1. A. [1] 0 [1] [2] 1 0
  2. B. [1, 2] 1 [1, 2] [2] 1 1
  3. C. [] 0 [1] [2] 1 0
  4. D. [1] 1 [1] [2] 1 1

Correct answer: A. [1] 0 [1] [2] 1 0

a.n.append mutates the shared class list; a.m += 1 creates an instance attribute on a. b.n = [2] creates an instance attribute on b that hides the class list.

More Python Programming questions

Exams that ask this topic