OOPs Interview Questions and Answers in Python
1. What is Object-Oriented Programming (OOP)?
OOP is a programming paradigm based on objects which contain data and methods to manipulate that data.
2. What are the main principles of OOP?
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
3. What are classes and objects in Python?
A class is a blueprint for creating objects. An object is an instance of a class.
4. How do you define a class in Python?
Example:
class Person:
def __init__(self, name):
self.name = name
5. What is the __init__ method in Python?
It's a constructor used to initialize object properties.
6. What is self in Python classes?
It refers to the instance of the class.
7. What is the difference between class variables and instance variables?
Class variables are shared; instance variables are specific to objects.
8. What is encapsulation and how is it implemented in Python?
OOPs Interview Questions and Answers in Python
Encapsulation hides internal object details using access modifiers.
9. How do you make a variable private in Python?
By prefixing with double underscores, e.g., __var.
10. What are getter and setter methods?
Methods used to access and update private attributes.
11. What are @property decorators used for in Python?
To turn a method into a property (read/write access).
12. What is inheritance in Python?
It enables one class to inherit attributes and methods from another.
13. How do you inherit a class in Python?
class Child(Parent):
14. What is the difference between single, multiple, and multilevel inheritance?
- Single: One parent
- Multiple: Multiple parents
- Multilevel: Inheritance chain
15. What is super() and how is it used?
Used to call parent class methods and constructors.
16. Can you override methods in Python? How?
Yes, by defining a method with the same name in the child class.
OOPs Interview Questions and Answers in Python
17. What is polymorphism in OOP?
It allows using the same method name for different types.
18. What is method overloading and does Python support it?
Python does not support it directly but can simulate with default arguments.
19. What is method overriding in Python?
Redefining a method in the child class.
20. What is duck typing in Python?
Object behavior is determined by methods rather than type.
21. What is the difference between compile-time and run-time polymorphism?
Compile-time (not in Python), run-time via method overriding.
22. What is abstraction in OOP?
Hiding complex logic and exposing essential features.
23. How is abstraction implemented in Python?
Using the abc module with abstract classes and methods.
24. What is the purpose of the abc module in Python?
To define abstract base classes.
25. How do you define an abstract class and abstract method in Python?
Use @abstractmethod decorator inside a class inheriting ABC.
OOPs Interview Questions and Answers in Python
26. What are dunder (magic) methods?
Special methods with __ prefix/suffix to define behavior like __init__, __str__.
27. What is the difference between __str__() and __repr__()?
__str__ is for user-friendly output, __repr__ is for debugging.
28. What are class methods and static methods?
Class methods take cls, static methods take no implicit first argument.
29. What is the use of @classmethod and @staticmethod decorators?
To define class-level or independent utility methods.
30. What is composition in OOP?
An object contains another object (has-a relationship).
31. Bank Account Class Example
class BankAccount:
def __init__(self, balance=0):
self.balance = balance
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
if amount <= self.balance:
self.balance -= amount
32. Class Hierarchy Using Inheritance
class Animal:
def sound(self):
OOPs Interview Questions and Answers in Python
print('Animal sound')
class Dog(Animal):
def sound(self):
print('Bark')
33. Private Attributes with Property Decorators
class Student:
def __init__(self, name):
self.__name = name
@property
def name(self):
return self.__name
@name.setter
def name(self, value):
self.__name = value
34. Override __add__ Method
class Point:
def __init__(self, x):
self.x = x
def __add__(self, other):
return Point(self.x + other.x)
35. Demonstrate Polymorphism
class Dog:
def speak(self): print('Bark')
class Cat:
def speak(self): print('Meow')
def call(animal):
animal.speak()