### Top 20 Python OOP Quiz 🐍
### Classes, Objects, Inheritance, Method Overriding & More
Test your Python knowledge with these 20 output-based MCQs covering classes, constructors, inheritance, method overriding, `super()`, polymorphism, encapsulation, and abstraction.
### Section 1: Classes and Objects
Q1. What is the output?
```python
class Student:
def __init__(self, name):
self.name = name
s = Student("Ravi")
print(s.name)
```
A. Student
B. Ravi
C. name
D. Error
Answer: B — Ravi. `self.name` stores the instance attribute.
Q2. What is the output?
```python
class Demo:
x = 10
a = Demo()
b = Demo()
a.x = 20
print(b.x)
```
A. 10
B. 20
C. None
D. Error
Answer: A — 10. Assigning `a.x` creates an instance attribute; `b.x` still resolves to the class attribute.
Q3. What does `__init__()` do?
A. Destroys an object
B. Initializes an instance after it is created
C. Inherits a class
D. Defines a static method
Answer: B — It initializes the newly created instance.
Q4. What is the output?
```python
class A:
def show(self):
print("A")
obj = A()
obj.show()
```
A. A
B. show
C. None
D. Error
Answer: A — A.
Q5. What is the output?
```python
class Counter:
count = 0
def __init__(self):
Counter.count += 1
a = Counter()
b = Counter()
print(Counter.count)
```
A. 0
B. 1
C. 2
D. Error
Answer: C — 2. The class variable is incremented for each instance.
### Section 2: Inheritance and Method Overriding
Q6. What is the output?
```python
class Parent:
def show(self):
print("Parent")
class Child(Parent):
pass
Child().show()
```
A. Child
B. Parent
C. None
D. Error
Answer: B — Parent. Child inherits the method.
Q7. What is the output?
```python
class Parent:
def show(self):
print("Parent")
class Child(Parent):
def show(self):
print("Child")
Child().show()
```
A. Parent
B. Child
C. Parent Child
D. Error
Answer: B — Child. The child overrides the parent method.
Q8. What is the output?
```python
class A:
def show(self):
print("A")
class B(A):
def show(self):
super().show()
print("B")
B().show()
```
A. B then A
B. A then B
C. B only
D. Error
Answer: B — A, then B. `super()` calls the next implementation in the method resolution order.
Q9. What is the output?
```python
class A:
def __init__(self):
print("A")
class B(A):
def __init__(self):
print("B")
B()
```
A. A then B
B. B then A
C. B only
D. A only
Answer: C — B only. The child constructor does not call the parent constructor.
Q10. What is the output?
```python
class A:
def __init__(self):
self.x = 10
class B(A):
def __init__(self):
super().__init__()
self.y = 20
obj = B()
print(obj.x + obj.y)
```
A. 10
B. 20
C. 30
D. Error
Answer: C — 30. `super().__init__()` initializes `x`; the child initializes `y`.
Q11. Which inheritance type allows a class to inherit from two parent classes?
A. Single inheritance
B. Multiple inheritance
C. Multilevel inheritance
D. Hierarchical inheritance
Answer: B — Multiple inheritance.
Q12. What is the output?
```python
class A:
def show(self):
print("A")
class B:
def show(self):
print("B")
class C(A, B):
pass
C().show()
```
A. A
B. B
C. A B
D. Error
Answer: A — A. Python searches base classes according to the MRO; A comes first.
### Section 3: Polymorphism, Encapsulation & Abstraction
Q13. What is the output?
```python
class Animal:
def sound(self):
print("Animal")
class Dog(Animal):
def sound(self):
print("Bark")
def make_sound(obj):
obj.sound()
make_sound(Dog())
```
A. Animal
B. Bark
C. Both
D. Error
Answer: B — Bark. This demonstrates runtime polymorphism.
Q14. What is the output?
```python
class A:
def show(self):
return "Hello"
obj = A()
print(obj.show())
```
A. None
B. Hello
C. show
D. Error
Answer: B — Hello. The method returns a string.
Q15. What is the output?
```python
class Employee:
def __init__(self):
self.__salary = 50000
e = Employee()
print(e.__salary)
```
A. 50000
B. None
C. AttributeError
D. SyntaxError
Answer: C — AttributeError. Double-underscore attributes undergo name mangling.
Q16. What is the output?
```python
class A:
def __init__(self):
self.__x = 10
obj = A()
print(obj._A__x)
```
A. 10
B. None
C. AttributeError
D. SyntaxError
Answer: A — 10. Name mangling changes `__x` to `_A__x`; it is not strict privacy.
Q17. Which module provides support for abstract base classes?
A. os
B. abc
C. sys
D. math
Answer: B — `abc` (Abstract Base Classes).
Q18. What happens when you run this code?
```python
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self):
pass
s = Shape()
```
A. Object created successfully
B. Returns None
C. TypeError
D. SyntaxError
Answer: C — TypeError. A class with unimplemented abstract methods cannot be instantiated.
Q19. What is the output?
```python
class A:
def greet(self):
print("Hello")
class B(A):
pass
class C(B):
pass
C().greet()
```
A. Hello
B. A
C. None
D. Error
Answer: A — Hello. This is multilevel inheritance: A → B → C.
Q20. What is the output?
```python
class A:
def show(self):
print("A")
class B(A):
def show(self):
print("B")
super().show()
class C(B):
def show(self):
print("C")
super().show()
C().show()
```
A. A B C
B. C B A
C. C A B
D. C only
Answer: B — C, B, A. Each `super()` call invokes the next method in the MRO.
### Quick Revision – Python OOP
| Concept | Key point |
| ------------- | ---------------------------------------------------------------------- |
| Class | Blueprint for creating objects |
| Object | Instance of a class |
| Inheritance | Reuse parent class functionality |
| Overriding | Child provides its own implementation of an inherited method |
| super() | Accesses the next implementation in the MRO |
| Polymorphism | Same interface, different behavior |
| Encapsulation | Bundling data and methods; name mangling helps avoid accidental access |
| Abstraction | Define required interfaces using abstract classes and methods |