What is the output of the following Python code snippet?
class A:
n = 0
def __init__(self):
A.n += 1
A(); A()
print(A.n)
n = 0
def __init__(self):
A.n += 1
A(); A()
print(A.n)
Correct answer
D. 2
D. 2
A class attribute is shared by all instances.
Asked in: Python Interview OOP
https://docs.python.org/3/tutorial/classes.htmlNot sure why the answer is D? Ask here and get an explanation.