Python OOP Interview Questions

Python OOP Interview Questions

1. What is Object-Oriented Programming (OOP)?

OOP is a programming paradigm based on the concept of “objects” that contain data and code. The main principles of OOP are:

  • Encapsulation
  • Inheritance
  • Polymorphism
  • Abstraction

2. What is a class in Python?

A class is a blueprint for creating objects. It defines a set of attributes and methods that the objects will have.

class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
return f"Hello, my name is {self.name}"

3. What is the difference between a class and an object?

  • A class is a blueprint/template that defines the structure and behavior
  • An object is an instance of a class with its own state and behavior
# Class definition
class Car:
def __init__(self, brand):
self.brand = brand
# Objects (instances)
car1 = Car("Toyota")
car2 = Car("Honda")

4. What is inheritance in Python?

Inheritance allows a class to inherit attributes and methods from another class. The class that inherits is called the child class, and the class being inherited from is called the parent class.

class Animal:
def speak(self):
pass
class Dog(Animal):
def speak(self):
return "Woof!"
class Cat(Animal):
def speak(self):
return "Meow!"

5. What are the types of inheritance in Python?

  1. Single Inheritance
  2. Multiple Inheritance
  3. Multilevel Inheritance
  4. Hierarchical Inheritance
  5. Hybrid Inheritance

6. What is polymorphism in Python?

Polymorphism allows objects of different classes to be treated as objects of a common superclass. It can be achieved through:

  • Method overriding
  • Method overloading (though not directly supported in Python)
class Shape:
def area(self):
pass
class Rectangle(Shape):
def area(self):
return self.width * self.height
class Circle(Shape):
def area(self):
return 3.14 * self.radius ** 2

7. What is encapsulation in Python?

Encapsulation is the bundling of data and methods that operate on that data within a single unit (class). In Python, it’s achieved through:

  • Public members (default)
  • Protected members (single underscore)
  • Private members (double underscore)
class BankAccount:
def __init__(self):
self.__balance = 0 # private
self._account_number = "12345" # protected
def deposit(self, amount):
self.__balance += amount

8. What is the difference between instance variables and class variables?

Instance variables:

  • Belong to instances of a class
  • Each instance has its own copy
  • Defined inside __init__

Class variables:

  • Shared by all instances
  • Defined outside any method
  • Can be accessed through class or instance
class Student:
school = "Python University" # class variable
def __init__(self, name):
self.name = name # instance variable

9. What is the difference between @staticmethod and @classmethod?

@staticmethod:

  • Doesn’t receive any implicit first argument
  • Can’t access or modify class state
  • Used for utility functions

@classmethod:

  • Receives the class as the first argument
  • Can access or modify class state
  • Used for alternative constructors
class MyClass:
@staticmethod
def static_method():
return "I'm static"
@classmethod
def class_method(cls):
return f"I'm a class method of {cls.__name__}"

10. What is method overriding?

Method overriding occurs when a child class provides a specific implementation of a method that is already defined in its parent class.

class Animal:
def make_sound(self):
return "Some sound"
class Dog(Animal):
def make_sound(self):
return "Woof!"

11. What is the difference between isinstance() and type()?

  • isinstance() checks if an object is an instance of a class or any of its parent classes
  • type() returns the exact type of an object
class Animal:
pass
class Dog(Animal):
pass
dog = Dog()
print(isinstance(dog, Animal)) # True
print(type(dog) == Animal) # False

12. What is the Method Resolution Order (MRO)?

MRO is the order in which Python looks for methods in a class hierarchy. It’s used to resolve method calls in multiple inheritance scenarios.

class A:
def method(self):
return "A"
class B(A):
def method(self):
return "B"
class C(A):
def method(self):
return "C"
class D(B, C):
pass
print(D.__mro__) # Shows the method resolution order

13. What is a property decorator?

The @property decorator is used to define a method that can be accessed like an attribute. It’s used for getters and setters.

class Person:
def __init__(self):
self._age = 0
@property
def age(self):
return self._age
@age.setter
def age(self, value):
if value < 0:
raise ValueError("Age cannot be negative")
self._age = value

14. What is the difference between __init__ and __new__?

  • __init__ is called after the object is created to initialize it
  • __new__ is called to create a new instance of the class
class MyClass:
def __new__(cls, *args, **kwargs):
print("Creating instance")
return super().__new__(cls)
def __init__(self, *args, **kwargs):
print("Initializing instance")

15. What is the difference between composition and inheritance?

Composition:

  • “Has a” relationship
  • More flexible
  • Better for code reuse
  • Loose coupling

Inheritance:

  • “Is a” relationship
  • More rigid
  • Can lead to tight coupling
  • Used for polymorphism
# Inheritance
class Animal:
def speak(self):
pass
class Dog(Animal):
def speak(self):
return "Woof!"
# Composition
class Engine:
def start(self):
return "Engine started"
class Car:
def __init__(self):
self.engine = Engine() # Car has an engine

Share & Connect