Q1 – Choose the correct built-in classes in Python.
Answer: List, int, Set
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.
Answer: Class
Q3 – State whether true or false:
In python, everything is an object. Thus everything would have either attributes or behavior or both.
Answer: ‘True’
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 __________________.
Answer: Encapsulation
Q5 – Consider the below given Python code.
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
Answer: Line1, Line3, Line4, Line5
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?
__init__()
Q3 – Create a class called ‘Mobile’ without any attributes or behavior.
class Mobile:
pass
Q4 – Observe the given ‘Student’ class and perform the below mentioned operation:
class Student:
def __init__(self):
self.name=””
self.mark=0.0
Create an object ‘student_obj’ for the class ‘Student’.
student_obj=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 _________________ .
Answer: reference variables
Q2 – What type of error we will get If we try to access a non-existing attribute?
Select one:
Answer: Attribute Error
Q3 – Attributes can be added to a class through a special function called__________
Select one:
Answer: __init__()
Q4 – What is the output of the below code snippet?
class Sample:
def __init__(self,num):
self.num=num
Q5 – Regina is a beginner in Python development. She has written the below code:
class Horse:
def __init__(self,name, weight):
self.name=name
self.__weight=weight
def get_weight(self):
return self.__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
YOU MUST CHOOSE TWO OPTIONS.
Answer: Change Line 9 to: print(“Horse name:”,horse1.name), Change Line 10 to : print(“Horse weight:”, horse1.get_weight())
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)
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):
self.id = id
self.name = name
self.location = location