Python OOP Interview question
1. What is Object-Oriented Programming (OOP) in Python?
Object-Oriented Programming (OOP) in Python is a programming
approach that models around objects, instances of classes which
encapsulate both data and objects. While it shares core principles with
general OOP, Python’s OOP has some unique features:
Dynamic Typing: Python classes and objects don’t require explicit type
declarations.
Everything is an object: Even basic data types (like integers, functions)
are objects with attributes and methods.
Multiple Inheritance: Python supports inheriting from multiple classes,
enabling complex class hierarchies.
Duck Typing: Python emphasizes behavior over type, allowing
polymorphism without strict type hierarchies.
Built-in Magic Methods: Python uses special methods
(like __init__, __str__, __len__) to customize object behavior, enabling
operator overloading and integration with Python syntax.
2. What are the key features of OOP?
The key features of oop are:
Encapsulation: Bundles data and methods within a class, hiding
internal details from outside access.
Abstraction: Hides complex implementation details and exposes only
necessary functionality.
Inheritance: Allows a new class to inherit properties and methods from
an existing class.
Polymorphism: Enables a single interface to represent different
behaviors.
3. What is a class and an object in Python?
Class: A blueprint for creating objects, defining their properties and
methods.
Object: An instance of a class.
4. What is the __init__ method in Python?
The __init__ method in Python is a special constructor method that
automatically runs when a new object of a class is created. It is used to
initialize the object’s attributes with default or user-provided values. It is
similar to constructors in other OOP languages like Java or C++.
5. What is self in Python classes?
self is a reference to the current instance of the class. It is used to access
attributes and methods of the class. When you define a method inside a
class, the first parameter of the method is always self, which allows the
method to refer to the object (or instance) on which the method was called.
6. What is the difference between instance variables and class
variables?
Instance variables belong to a specific object of a class and are defined
inside methods using self (e.g., [Link]). Each object gets its own copy,
so modifying one instance’s variable does not affect others.
Class variables, on the other hand, are defined directly inside the class
but outside methods. They are shared across all objects of that class, so
changing a class variable affects every instance (unless it is overridden
at the instance level).
7. What is inheritance in Python?
Inheritance allows a class to inherit attributes and methods from another
class. Inheritance enables the child class to inherit the properties
(attributes) and behaviors (methods) of the parent class, and it can also
define its own additional attributes and methods or override existing ones.
8. What is method overloading in Python?
In languages like Java or C++, method overloading allows multiple
methods with the same name but different parameters. Python does not
support true method overloading. If you define two methods with the
same name, the latest one will overwrite the previous definition.
However, Python provides similar flexibility using default arguments or
variable-length arguments like *args and **kwargs to handle variable
numbers of arguments in a single method.
9. What is method overriding in Python?
Method overriding allows a subclass to provide a specific implementation
of a method that is already defined in its superclass. This means that the
subclass can "override" the behavior of the method inherited from the
parent class, providing a different version that is more suitable for the
subclass.
10. What is polymorphism in Python?
Polymorphism allows objects to be treated as instances of their parent class,
enabling different implementations for the same interface. It enables a single
function, method, or operator to work with different types of objects in a
way that is determined at runtime, allowing code to be more flexible and
reusable.
11. What is encapsulation, and how does Python achieve it?
Encapsulation is an OOP principle that restricts direct access to an object’s
internal data and exposes it only through controlled interfaces. It helps
protect the internal state of an object and ensures data integrity.
In Python, encapsulation is achieved using naming conventions:
Single underscore _var: Indicates a protected attribute (by convention,
should not be accessed outside the class).
Double underscore __var: Makes an attribute private using name
mangling, which makes it harder to access from outside the class.
12. What is the super() function in Python?
super() allows access to methods of the parent class and it’s often used to call
the parent class’s constructor.
13. What are abstract classes in Python?
Abstract class is a class that serves as a blueprint for other classes. It defines
a structure that derived classes must follow but does not provide full
implementation, abstract classes cannot be instantiated directly which
means they are meant to be subclassed.
14. What is multiple inheritance in Python?
Multiple inheritance allows a subclass to inherit features from multiple
parent classes, making it more versatile and capable of combining
behaviors from different sources.
15. What is the diamond problem in multiple inheritance? How
does Python handle it?
The Diamond Problem in multiple inheritance is a classic issue that arises
when a class inherits from two classes that both inherit from a common
ancestor. The problem occurs because, in such scenario, it's unclear which
version of the inherited method should be invoked when the method is
called on the subclass.
16. What are class methods in Python?
Class methods are methods that are bound to the class rather than its
instances. They take the class itself as the first parameter, conventionally
named cls, instead of self. Class methods are defined using
the @classmethod decorator and can access or modify class-level attributes.
17. What is the difference between staticmethod and classmethod?
In Python, both @staticmethod and
@classmethod @staticmethod and @classmethod are used to define methods that
are not bound to the instance of the class (i.e., they can be called on the
class itself).
Aspect
staticmethod classmethod
Not bound to either the Bound to the class, not the
Binding
instance or the class. instance.
Takes cls as the first
Does not take self or cls
First Argument argument (refers to the
as the first argument.
class).
Access to Cannot access or Can access and modify class
Class/Instance modify instance or variables (but not instance
Variables class variables. variables).
18. What are magic methods in Python?
Magic methods (also called dunder methods) are special methods in
Python that start and end with double underscores.
They allow customization of object behavior, such as how objects are
represented, compared, or used with operators.
Examples:
__init__: Constructor method, called when an object is created.
__str__: Defines the string representation of an object.
__len__: Defines behavior for the len() function.
__add__: Enables operator overloading for +.
19. How does Python handle garbage collection?
Python uses automatic garbage collection to manage memory by
identifying and removing unused objects and It employs reference
counting and a cyclic garbage collector.
20. What is metaclass in Python?
A metaclass is a class of a class that defines how classes behave and classes
are instances of metaclasses. Essentially, metaclasses define the "rules" for
building classes, much like a class defines the rules for creating objects.
21. How is data hiding implemented in Python?
Data hiding is implemented using name mangling to make attributes less
accessible from outside the class. Attributes prefixed with double
underscores (__) are internally renamed to include the class name, which
discourages direct access.
[Link] is the purpose of __slots__ in Python classes?
__slots__ attribute is used to explicitly declare the allowed instance
attributes in a class, preventing the creation of a default __dict__ for each
instance. This has two main benefits:
Memory optimization: Instances consume less memory because no per-
object __dict__ is created.
Attribute restriction: Only the attributes listed in __slots__ can be
assigned to instances, helping prevent accidental creation of new
attributes.
23. What is the Global Interpreter Lock (GIL) and its impact on
Python OOP?
The Global Interpreter Lock (GIL) is a mutex that allows only one thread to
execute Python bytecode at a time, even on multi-core systems.
It's impact:
For CPU-bound tasks, multithreading is limited by the GIL, so
performance gains are minimal.
For I/O-bound tasks (like file handling, networking, databases), threads
still help because the GIL is released during I/O operations.
[Link] do you achieve operator overloading in Python?
Operator overloading lets you redefine how operators (like +, -, *) behave
for user-defined classes. This is done by defining special methods (also
called magic/dunder methods) inside your class.
25. What is the difference between composition and inheritance?
Composition and inheritance are two fundamental object-oriented
programming (OOP) techniques for creating relationships between classes.
The main difference between inheritance and composition is that:
Inheritance is when a class derives from another class and automatically
gets its behavior. It represents an “is-a” relationship. For example, a Car
class inheriting from a Vehicle class.
Composition is when a class contains objects of other classes and uses
them to achieve functionality. It represents a “has-a” relationship. For
example, a Car class having an Engine object.