Here’s a complete OOP interview questions set for Python — from basic to advanced — with ✅ real-world relevance, 🧠 conceptual focus, and 🧪 coding triggers. You can practice or review these inline (Notion/blog-style ready).
🧠 Python OOP Interview Questions (With Hints)
🔹 Basic Level (Conceptual Clarity)
1. What is the difference between a class and an object?
- 🔸 Hint: Think of blueprint vs instance.
2. What is self
in Python classes?
- 🔸 Hint: Refers to the instance calling the method.
3. What does the __init__()
method do?
- 🔸 Hint: Think constructor/initializer.
4. How is @staticmethod
different from @classmethod
?
- 🔸 Hint: Focus on parameters (
self
,cls
, none).
5. What is encapsulation and how is it implemented in Python?
- 🔸 Hint: Using
_protected
or__private
attributes.
🔹 Intermediate Level (Design + Usage)
6. What is inheritance and how is it used in Python?
- 🔸 Hint:
class Dog(Animal)
→ Dog inherits from Animal.
7. What is multiple inheritance? Any issues with it?
- 🔸 Hint: Method Resolution Order (MRO) and
super()
.
8. Explain polymorphism with an example.
- 🔸 Hint: Same method name, different behavior in subclasses.
9. What is the difference between overloading and overriding?
- 🔸 Hint: Overloading = same method with different args (limited in Python); Overriding = subclass changes parent behavior.
10. What is composition in OOP? How is it better than inheritance sometimes?
- 🔸 Hint: “Has-a” relationship. Flexible over tight coupling.
🔹 Advanced Level (Dunder + Architecture)
11. What are dunder methods? Why are they useful?
- 🔸 Hint:
__str__
,__len__
,__eq__
,__getitem__
, etc.
12. What does the __call__()
method do?
- 🔸 Hint: Allows object to be used like a function.
13. What’s the difference between __str__
and __repr__
?
- 🔸 Hint:
str
is readable,repr
is for debugging (dev-facing).
14. How can you make a class iterable?
- 🔸 Hint: Implement
__iter__()
and__next__()
.
15. How do you implement abstraction in Python?
- 🔸 Hint: Use
abc.ABC
and@abstractmethod
.
🔹 Real-World + Design Questions
16. How would you design a logging system using OOP?
- 🔸 Hint: Use base
Logger
class and subclasses forFileLogger
,DBLogger
.
17. What design patterns use inheritance or composition?
- 🔸 Hint: Inheritance → Template, Strategy; Composition → Decorator, Adapter.
18. When should you prefer composition over inheritance?
- 🔸 Hint: When you want flexibility and loose coupling.
19. How do you protect class attributes from being modified directly?
- 🔸 Hint: Use
@property
, private variables (__balance
), setters.
20. What are dataclasses
in Python? How do they help?
- 🔸 Hint: Auto-generate boilerplate code (
__init__
,__repr__
, etc.).
🔹 Practical Coding Prompts
🧪 Q21. Write a class BankAccount
with deposit, withdraw, and balance check. Use private attributes.
🧪 Q22. Write a base class Shape
and subclasses like Circle
, Rectangle
with their own area()
methods.
🧪 Q23. Create a Car
class that has an Engine
object using composition. Call engine.start()
from car.
🧪 Q24. Create a Logger
base class. Create FileLogger
and ConsoleLogger
subclasses using polymorphism.
🧪 Q25. Implement a class where calling the object increments a counter (__call__()
).
📘 Bonus: Quick Tips
- ✅
@classmethod
→ Used for alternate constructors (from_json
,from_dict
) - ✅
@staticmethod
→ Pure functions that belong to a class for grouping - ✅ Prefer composition when behavior needs to be reused rather than extended
- ✅ Use
super()
to call parent methods in child class overrides
Absolutely! Here’s your inline sample answer key to the 25 Python OOP interview questions above — clean, short, and suitable for review, Notion, or blog use.
📝 Python OOP Interview – Sample Answer Key
🔹 Basic Level
1. Class vs Object
- Class: A blueprint for creating objects.
- Object: An instance of a class with actual data.
2. What is self
?
- Refers to the current instance of the class.
- Used to access instance variables and methods.
3. What is __init__()
?
- The constructor method called when an object is created.
- Initializes object attributes.
4. @staticmethod
vs @classmethod
@staticmethod
: Noself
orcls
, behaves like a regular function inside class.@classmethod
: Receivescls
, can access/modify class state.
5. Encapsulation
- Hides internal data using
_protected
or__private
variables. - Achieved via access modifiers and getter/setter methods.
🔹 Intermediate Level
6. Inheritance
- One class inherits methods/attributes from another.
- Promotes code reuse.
class Dog(Animal):
pass
7. Multiple Inheritance & MRO
- A class inherits from multiple parent classes.
- Python resolves method conflicts using MRO (Method Resolution Order).
class A: pass
class B: pass
class C(A, B): pass
8. Polymorphism
- Multiple classes have methods with the same name but different behaviors.
def speak(animal): animal.speak()
9. Overloading vs Overriding
- Overriding: Subclass redefines parent method.
- Overloading: Not natively supported; achieved with default args or
*args
.
10. Composition
- One class uses another class inside it (HAS-A).
- Preferred over inheritance for flexibility.
class Car:
def __init__(self):
self.engine = Engine()
🔹 Advanced Level
11. Dunder Methods
- Special methods with
__
prefix/suffix (e.g.,__init__
,__str__
). - Define object behavior in built-in operations.
12. __call__()
- Makes object behave like a function.
class Counter:
def __call__(self): ...
13. __str__
vs __repr__
__str__
: Human-readable (print)__repr__
: Debug/developer-friendly
14. Iterable Object
class MyList:
def __iter__(self): ...
def __next__(self): ...
15. Abstraction
- Hide implementation using abstract base class:
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self): pass
🔹 Real-World + Design
16. Logger System
class Logger: ...
class FileLogger(Logger): ...
class DBLogger(Logger): ...
17. Design Patterns
- Inheritance: Strategy, Template
- Composition: Adapter, Decorator
18. Composition > Inheritance
- Use composition when behaviors are modular and interchangeable.
19. Protecting Attributes
class User:
def __init__(self): self.__age = 0
def get_age(self): return self.__age
20. Dataclasses
from dataclasses import dataclass
@dataclass
class Point:
x: int
y: int
🔹 Coding Practice Questions (Samples)
21. BankAccount Class
class BankAccount:
def __init__(self):
self.__balance = 0
def deposit(self, amt): self.__balance += amt
def get_balance(self): return self.__balance
22. Shape → Circle, Rectangle
class Shape(ABC):
@abstractmethod
def area(self): pass
class Circle(Shape):
def area(self): return 3.14 * 5 * 5
23. Car-Engine Composition
class Engine:
def start(self): print("Engine on")
class Car:
def __init__(self): self.engine = Engine()
def drive(self): self.engine.start()
24. Logger Polymorphism
class Logger:
def log(self, msg): pass
class FileLogger(Logger):
def log(self, msg): print("File:", msg)
25. __call__()
Example
class Counter:
def __init__(self): self.count = 0
def __call__(self): self.count += 1; return self.count
Leave a Reply