Python Interview Questions

Python Interview Questions - Modules & OOP

Master Python interview questions on Modules, Exception Handling, and Object-Oriented Programming (OOP).

By TechCoder TeamLast updated: 2026-06-02
In a Nutshell

Master Python interview questions on Modules, Exception Handling, and Object-Oriented Programming (OOP). This interview-focused guide covers essential python interview questions - modules & oop concepts for technical interviews.

Python Interview Questions – Level 3: Modules, OOP & Exceptions

This level dives into software architecture, error resilience, and the core principles of Object-Oriented Programming in Python.


121. What is a module?

A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended.

122. What is import?

The import statement is used to make code available from another module.

123. Difference between import and from import?

  • import module: Imports the entire module. You access its contents via module.function().
  • from module import function: Imports specific attributes. You access them directly as function().

124. What is __init__.py?

__init__.py is used to mark a directory as a Python package. It is executed when the package or a module in the package is imported. In Python 3.3+, it's optional, but still recommended for initialization logic.

125. What is a package?

A package is a way of structuring Python’s module namespace by using "dotted module names". It is essentially a directory containing a collection of modules and an __init__.py file.

126. What is sys module?

The sys module provides access to variables used or maintained by the interpreter and functions that interact strongly with the interpreter (e.g., sys.path, sys.argv).

127. What is os module?

The os module provides a portable way of using operating system-dependent functionality like interacting with the file system.

128. What is datetime module?

Used for manipulating dates and times with classes like date, time, datetime, and timedelta.

129. What is random module?

Used to generate pseudo-random numbers and select random elements from sequences.

130. What is math module?

Provides access to mathematical functions like sqrt(), sin(), and constants like pi.

131. What is exception?

An exception is an error that occurs during program execution. Python generates an exception that can be handled to prevent a crash.

132. What is try-except?

The try-except block is used to catch and handle exceptions.

try:
    x = 1 / 0
except ZeroDivisionError:
    print("Cannot divide by zero!")

133. What is finally?

The finally block contains code that always executes, regardless of whether an exception occurred. It's used for cleanup actions.

134. What is raise?

The raise keyword is used to manually trigger an exception.

135. What are built-in exceptions?

Standard exceptions like IndexError, KeyError, ValueError, and TypeError.

136. What is custom exception?

A user-defined exception created by inheriting from the built-in Exception class.

137. What is OOP?

Object-Oriented Programming (OOP) is a paradigm based on "objects" that contain data (attributes) and code (methods).

138. What is class?

A class is a blueprint or template for creating objects.

139. What is object?

An object is an instance of a class.

140. What is __init__ method?

The __init__ method is the constructor of a class, called automatically when a new object is created.

141. What is self?

self represents the instance of the class, allowing access to its attributes and methods.

142. What is instance variable?

Variables unique to each instance, typically defined in __init__ using self.

143. What is class variable?

Variables shared by all instances of a class, defined directly in the class body.

144. What is inheritance?

Allows a class (child) to acquire properties and methods from another class (parent).

145. Types of inheritance?

Includes Single, Multiple, Multilevel, Hierarchical, and Hybrid inheritance.

146. What is method overriding?

When a subclass provides a new implementation for a method already defined in its parent class.

147. What is super()?

A function used to call methods from the parent class, often used for initialization.

148. What is polymorphism?

Allows different classes to be treated as instances of the same class through inheritance.

149. What is encapsulation?

Wrapping data and methods into a single unit (class) and restricting direct access (data hiding).

150. What is abstraction?

Hiding complex implementation details and showing only essential features, often using Abstract Base Classes (ABC).

151. What is private variable?

Achieved using a double underscore prefix (__var), which triggers name mangling in Python.

152. What is protected member?

Indicated by a single underscore (_var), signaling that it's intended for internal use.

153. What is multiple inheritance?

When a class inherits from more than one parent class.

154. What is method resolution order (MRO)?

The order in which Python searches for methods in a class hierarchy, determined by the C3 Linearization algorithm.

155. What is __str__?

Returns a user-friendly string representation of an object.

156. What is __repr__?

Returns an "official" string representation of an object, used for debugging.

157. What is isinstance()?

Checks if an object is an instance of a specific class or its subclasses.

158. What is issubclass()?

Checks if a class is a derived class of another class.

159. What is dataclass?

A decorator that automatically generates __init__, __repr__, etc., for classes that primarily store data.

160. What is namedtuple?

A factory function from collections for creating tuple subclasses with named fields.