Python - Classes

Why should I learn to solve Python: Classes technical interview questions?

Learn and practise solving Python: Classes technical interview questions and answers to enhance your skills for clearing technical interviews, HR interviews, campus interviews, and placement tests.

Where can I get technical Python: Classes technical interview questions and answers with explanations?

IndiaBIX provides you with lots of fully solved Python: Classes technical interview questions and answers with a short answer description. You can download Python: Classes technical interview questions and answers as PDF files or e-books.

How do I answer Python: Classes technical interview questions from various companies?

You can answer all kinds of Python: Classes technical interview questions by practising the given exercises (short answer type). You can also find the frequently asked Python: Classes technical interview questions with answers from various companies, such as TCS, Wipro, Infosys, CTS, IBM, etc.

1.
What is a class in Python?

In Python, a class is a blueprint for creating objects. Objects have member variables and have behavior associated with them. In Python, a class is created by the keyword class.

Let's create a simple class called Person with a constructor, a method, and a member variable.

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def display_info(self):
        print(f"Name: {self.name}, Age: {self.age}")

# Creating an object of the Person class
person_obj = Person("John", 25)

# Calling the display_info method of the Person class
person_obj.display_info()

In this example, we defined a class Person with a constructor __init__ that initializes the member variables name and age. The class also has a method display_info to display the information of a person.

We then created an object person_obj of the Person class and called the display_info method to print the person's information.

Output:

Name: John, Age: 25

2.
Explain the difference between a class and an object

In Python, a class is a blueprint or a template for creating objects. It defines a data structure that encapsulates data and the methods that operate on that data. On the other hand, an object is an instance of a class. It is a concrete realization of the class, with actual values in place of the placeholders defined by the class.

Let's illustrate the difference with an example:

class Dog:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def bark(self):
        print("Woof!")

# Creating objects of the Dog class
dog1 = Dog("Buddy", 3)
dog2 = Dog("Charlie", 5)

# Accessing object attributes
print(f"{dog1.name} is {dog1.age} years old.")
print(f"{dog2.name} is {dog2.age} years old.")

# Calling object methods
dog1.bark()
dog2.bark()

In this example, Dog is a class that defines a template for creating dog objects. The __init__ method is a constructor that initializes the object's attributes, and the bark method is a behavior associated with the dog objects.

We then create two dog objects, dog1 and dog2, which are instances of the Dog class. We access their attributes (name and age) and call their methods (bark).

Output:

Buddy is 3 years old.
Charlie is 5 years old.
Woof!
Woof!

3.
How do you define a class in Python?

In Python, a class is defined using the class keyword, followed by the class name and a colon. Inside the class block, you can define attributes and methods. The __init__ method is a special method used for initializing object attributes when an object is created.

Let's create a simple class called Car to illustrate the definition of a class:

class Car:
    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year

    def display_info(self):
        print(f"{self.year} {self.make} {self.model}")

# Creating an object of the Car class
my_car = Car("Toyota", "Camry", 2022)

# Accessing object attributes
print(f"My car is a {my_car.year} {my_car.make} {my_car.model}.")

# Calling object method
my_car.display_info()

In this example, the Car class has an __init__ method that initializes the make, model, and year attributes. It also has a method called display_info that prints information about the car.

We then create an object my_car of the Car class and access its attributes (make, model, year) and call its method (display_info).

Output:

My car is a 2022 Toyota Camry.
2022 Toyota Camry

4.
Discuss the purpose of the __init__method in a class.

In Python, the __init__ method is a special method used in a class to initialize the object's attributes when an object is created. It is also known as the constructor method. The purpose of the __init__ method is to set the initial state of the object by assigning values to its attributes. This method is called automatically when an object is created from the class.

Let's illustrate the use of the __init__ method with an example:

class Dog:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def display_info(self):
        print(f"Name: {self.name}, Age: {self.age}")

# Creating objects of the Dog class
dog1 = Dog("Buddy", 3)
dog2 = Dog("Charlie", 5)

# Accessing object attributes
dog1.display_info()
dog2.display_info()

In this example, the __init__ method of the Dog class takes two parameters (name and age) and initializes the object's attributes with the provided values. When we create objects dog1 and dog2 of the Dog class, the __init__ method is automatically called to set the initial state of the objects.

Output:

Name: Buddy, Age: 3
Name: Charlie, Age: 5