Python Classes and Objects Question and Answer

Python  Classes  and  Objects Quiz
Python Classes and Objects Quiz

Pre-Quiz classes and objects

Q1 – Choose the correct built-in classes in Python.

Q2 – A ______________ is a classication of certain objects and it is just a description of the properties and behavior of all objects of that classication should possess.

Q3 – State whether true or false:
In python, everything is an object. Thus everything would have either attributes or behavior or both.

Q4 – List1=[4,6,2,8]
List1.reverse() – to reverse the list.
It is not necessary that we should know how the reverse() method is working in order to use it in our list. This ability to use something without having to know the details of how it is working is called as __________________.

Q5 – Consider the below given Python code.
Python code of line numbers
Python code of line numbers
Identify the line numbers that will be covered when the function, func is invoked using the below test data.
a=3,b=5 and c=1

Practice Session – Class and Objects

Q1 – Fill the code for a getter method for ‘salary’ which is a private attribute:
def get_salary(self):

return self.__salary

Q2 – When we create an object, which function inside the class of that object is invoked automatically?


Q3 – Create a class called ‘Mobile’ without any attributes or behavior.

class Mobile:


Q4 – Observe the given ‘Student’ class and perform the below mentioned operation:
class Student:
  def __init__(self):””
Create an object ‘student_obj’ for the class ‘Student’.


Post Quiz – Class and Objects

Q1 – We need variables to access and reuse the objects that we create. Such variables that are used to access objects are called _________________ .

Q2 – What type of error we will get If we try to access a non-existing attribute?
Select one:

Q3 – Attributes can be added to a class through a special function called__________
Select one:

Q4 – What is the output of the below code snippet?
class Sample:
  def __init__(self,num):

  def set_num(self,num):

  def get_num(self):
   return self.num


Select one:

Q5 – Regina is a beginner in Python development. She has written the below code:
class Horse:
  def __init__(self,name, weight):
  def get_weight(self):
   return self.__weight

print(“Horse name:”,horse1.__name)
print(“Horse weight:”, horse1.__weight)

but, she is unable to proceed due to an error. Which of the following steps should she follow to get the desired output species below?
Horse name: Jaguar
Horse length: 75.0

Check Your Understanding – Class and Objects

Q1 – A class ‘Athlete’ is given below: You have to perform the following tasks:
 1. Make all the attributes private
 2. Complete the necessary parts of accessor and mutator methods for name attributes
 3. Create an athlete object ‘runner’ with the name as ‘Jane’and gender as ‘Female’ and make her run
Fill only the necessary parts:
class Athlete:
  def __init__(self,name,gender)



def get_name(self)

return self.__name

def set_name(self, name):


def running(self):
   print(“150mtr running”)
  print(“200mtr running”)



Q2 – We have a list of customer objects. Re-arrange the code to get output given below:
(We have a dictionary of customer objects based on location)
Output should be like:
Location: England, Name: Reagan, Id: 1
Location: India, Name: Ram, Id: 5
Location: Singapore, Name: Kate, Id: 8

 class Customer:
def __init__(self, id, name, location): = id = name
self.location = location

customer_list = [Customer(1, ‘Reagan’, ‘England’), Customer(5, ‘Ram’, ‘India’), Customer(8,’Kate’, ‘Singapore’)]

customer_dict = {}
for customer in customer_list:

customer_dict[customer.location] = customer
for key,value in customer_dict.items()

print (“Location: “+key+”, Name: “+value.cust_name+”, Id: “+(str(value.cust_id)))

Q3 – Arrange the below code to get the given output:
Output should be like:
Mobile price: 30000

class Mobile:
def __init__(self, price, brand):

self.price = price
self.brand = brand

def change_price(mobile_obj):

mobile_obj.price = 30000

mobile1=Mobile(24000, “Lenovo”)


print (“Mobile Price:”,mobile1.price)

Q4 – What is the output of the following code snippet?
class Customer:
  def __init__(self, cust_id, age):
   self.cust_id = cust_id
   self.age = age


def change(customer2):


Q5 – Match the Following:

Static attributes are accessed using

Class Name

“It is a classication of certain objects and it is just a description of the properties and behavior all objects of that classication should possess”.


In python, adding a double underscore (__ ) changes the name of the attribute to


We can create shared attributes by placing them directly inside the class and not inside the constructor. These shared attributes are known as:

Class attributes

This decorator method helps to access the specied method using that class objects


Prevent access to a data outside the class


Recommended :

  1. Employee Salary
  2. Book Details
  3. Savings Account
  4. The Circle Object
  5. Vehicle Premium

Leave a Comment

Your email address will not be published. Required fields are marked *