Python OOP Interview Questions (Top 50+)
Object-Oriented Programming (OOP) is a core concept in Python and is frequently asked in interviews. It helps in structuring programs using classes and objects.
This guide covers the most important Python OOP interview questions with examples and explanations.
1. OOP Basics
Q1. What is OOP?
OOP is a programming paradigm based on objects and classes.
Q2. What is a class?
A class is a blueprint for creating objects.
Q3. What is an object?
An object is an instance of a class.
Q4. Define encapsulation.
Encapsulation is binding data and methods together.
2. Class and Object Questions
class Person:
def __init__(self, name):
self.name = name
p = Person("John")
print(p.name)
Q5. What is __init__?
It is a constructor used to initialize objects.
3. Inheritance Questions
class A:
def show(self):
print("A")
class B(A):
pass
b = B()
b.show()
Q6. Types of inheritance?
Single, multiple, multilevel, hierarchical.
4. Polymorphism Questions
def add(a, b, c=0):
return a + b + c
print(add(1,2))
print(add(1,2,3))
Q7. What is polymorphism?
Ability to take multiple forms.
5. Abstraction Questions
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self):
pass
Q8. What is abstraction?
Hiding implementation details.
6. Encapsulation Questions
class Test:
def __init__(self):
self.__x = 10
def get(self):
return self.__x
Q9. What is private variable?
Variable prefixed with __ (double underscore).
7. Advanced OOP Questions
Q10. What is method overloading?
Not directly supported, achieved using default arguments.
Q11. What is method overriding?
Child class redefining parent method.
class A:
def show(self):
print("A")
class B(A):
def show(self):
print("B")
8. Coding Problems
Problem 1: Create class with methods
class Calc:
def add(self, a, b):
return a + b
Problem 2: Bank Account class
class Bank:
def __init__(self, balance):
self.balance = balance
def deposit(self, amt):
self.balance += amt
9. Common Mistakes
1. Confusing class and object.
2. Not using self keyword.
3. Misunderstanding inheritance.
4. Ignoring encapsulation.
10. Interview Tips
1. Understand OOP concepts clearly.
2. Practice coding examples.
3. Explain concepts with real-life examples.
4. Focus on implementation.
Conclusion
OOP is a fundamental concept in Python interviews. Mastering classes, inheritance, and polymorphism will help you crack interviews easily.
Practice regularly and build projects using OOP concepts.
Codecrown