1. Home
  2. Data Science
  3. Python
  4. What is the output of the following Python code snippet?

Added 25 Nov 2026

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)
  1. 1
  2. 0
  3. Error
  4. 2 Correct answer

Correct answer

D. 2

MCQSEXAM

Explanation

A class attribute is shared by all instances.

Reference

Asked in: Python Interview OOP

https://docs.python.org/3/tutorial/classes.html

Preparing for an exam?

Doubts & discussion

Not sure why the answer is D? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More Python MCQs

Practise all Python MCQs

Similar MCQs