Object-Oriented Programming · Question 23 · Medium
What is the output of the following Python code?
class Counter:
count = 0
def __init__(self):
Counter.count += 1
a = Counter()
b = Counter()
a.count = 10
c = Counter()
print(Counter.count, a.count, b.count)
- 3 10 3
- 3 3 3
- 10 10 3
- 2 10 2
Answer and explanation
Answer: A. 3 10 3
Explanation: Each constructor call increments the class variable Counter.count, so it becomes 3. a.count = 10 creates an instance attribute on a that hides the class variable for a only; b.count still reads the class variable, 3.
Previous: What is the output of the following C++ program? · Next: Which SOLID principle says a class should have only one reason to change?
Practise object-oriented programming
Try a fresh set of object-oriented programming questions with an explanation after every answer, free and without signing up: Object-Oriented Programming practice. Or sit a full placement mock test.
More object-oriented programming questions
- Which OOP concept means exposing only the essential features of an object…
- What is the output of the following C++ program?
- What is the output of the following C++ program?
- Which SOLID principle says a class should have only one reason to change?
- Which design pattern ensures that a class has only one instance and provides a…
- A House is made of Rooms, and the Rooms cannot exist without the House: when…
- What is the output of the following Java code?
- What is the output of the following C++ program?
- What is the output of the following Python code?
- What is the output of the following Java code?
More technical & programming practice
DBMS · SQL · Operating Systems · Computer Networks · Software Engineering · Cloud & DevOps · GenAI Basics · Pseudo-code
LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge