Data Science & ML

Python Object-Oriented Programming

Classes, objects, inheritance, encapsulation, polymorphism, special methods, decorators

20 interview questionsยท
Junior
1

What is a class in Python?

Answer

A class is a blueprint for creating objects that share the same attributes and methods. It defines the structure and behavior of objects that will be instantiated from it. Classes allow organizing code in a modular and reusable way by grouping related data and functionality together.

2

What is the role of the __init__ method in a Python class?

Answer

The __init__ method is the class constructor. It is automatically called when creating a new instance and allows initializing object attributes with specific values. This is where you typically define the initial state of the object by assigning values to instance attributes via self.

3

What does the self parameter represent in class methods?

Answer

The self parameter represents the current instance of the class. It allows accessing object attributes and methods from within the class. While the name self is a convention, it is strongly recommended to use it for code readability. Python automatically passes the instance as the first argument when calling a method.

4

What is the difference between a class attribute and an instance attribute?

5

How to create an instance of a class named Person in Python?

+17 interview questions

Master Data Science & ML for your next interview

Access all questions, flashcards, technical tests, code review exercises and interview simulators.

Start for free