MCQs Classes and Objects
1. What is setattr() used for?
- To set or change the value of an attribute of object
- To access the attribute of the object
- To set an attribute for new class
- To set an attribute inside the function
Ans: To set or change the value of an attribute of object
2. What is getattr() used for?
- To access the attribute of the object
- To get the default value of an attribute
- To get an attribute of class with no object
- To check if an attribute exists or not
Ans: To access the attribute of the object
3. The assignment of more than one function to a particular operator is _______.
- Operator over-assignment
- Operator overriding
- Operator overloading
- Operator instance
Ans: Operator overloading
4. Which of the following is not a class method?
- Bounded
- Unbounded
- Non-static
- Static
Ans: Non-static
5. What is hasattr(obj,name) used for?
- To check if an attribute exists or not
- To check an object of a class
- To check whether the attribute is an integer or not
- To check the attribute is assigned with value or not
Ans: To check if an attribute exists or not
6. What is delattr(obj,name) used for?
- To delete an attribute along with that object
- To delete the value of the attribute
- To check if an attribute is deleted or not
- To delete an attribute
Ans: To delete an attribute
7. __del__ method is used to destroy instances of a class. True or False?
- FALSE
- TRUE
Ans: TRUE
8. What does print(Test.__name__) display (assuming Test is the name of the class) ?
- Exception is thrown
- __main__
- ()
- Test
Ans: Test
9. What is the output of the following code?
class change:
def __init__(self, x, y, z):
self.a = x + y + z
x = change(1,2,3)
y = getattr(x, 'a')
setattr(x, 'a', y+1)
print(x.a)
- 0
- 7
- 8
- 6
Ans: 7
10. What is the output of the following piece of code?
class objects:
def __init__(self):
self.colour = None
self._shape = "Circle"
def display(self, s):
self._shape = s
obj=objects()
print(obj._objects_shape)
- Error because the ‘objects’ object has no attribute ‘_objects_shape’
- The program runs fine because name mangling has been properly implemented
- Error because the proper syntax for name mangling hasn’t been implemented
- Error because the member shape is a protected member
Ans: Error because the ‘objects’ object has no attribute ‘_objects_shape’
11. What is the output of the following code?
class Demo:
def __init__(self):
self.a = 1
self.__b = 1
def display(self):
return self.__b
obj = Demo()
print(obj.__b)
- The program has an error because b is private and display(self) is returning a private member
- The program runs fine and 1 is printed
- The program has an error because there isn’t any function to return self.a
- The program has an error because b is private and hence can’t be printed
Ans: The program has an error because b is private and hence can’t be printed
12. What is the output of the following code?
class Demo:
def __init__(self):
self.a = 1
self.__b = 1
def get(self):
return self.__b
obj = Demo()
print(obj.get())
- The program has an error because b is private and hence can’t be printed
- The program runs fine and 1 is printed
- The program has an error because b is private and display(self) is returning a private member
- The program has an error because there isn’t any function to return self.a
Ans: The program runs fine and 1 is printed
13. What is the output of the following code?
class test:
def __init__(self):
self.variable = 'Old'
self.Change(self.variable)
def Change(self, var):
var = 'New'
obj=test()
print(obj.variable)
- ‘Old’
- ‘New’ is printed
- Error because function change can’t be called in the __init__ function
- Nothing is printed
Ans: ‘Old’
14. Is the following piece of code correct?
class A:
def __init__(self,b):
self.b=b
def display(self):
print(self.b)
obj=A("Hello")
del obj
- TRUE
- FALSE
Ans: TRUE
15. What is the output of the following code?
class Demo:
def __init__(self):
pass
def test(self):
print(__name__)
obj = Demo()
obj.test()
- Demo
- test
- __main__
- Exception is thrown
Ans: __main__
16. What is the output of the following code?
class demo():
def __repr__(self):
return '__repr__ built-in function called'
def __str__(self):
return '__str__ built-in function called'
s=demo()
print(s)
- __str__ built-in function called
- __repr__ built-in function called
- Error
- Nothing is printed
Ans: __str__ built-in function called
17. What is the output of the following piece of code?
class stud:
'Base class for all students'
def __init__(self, roll_no, grade):
self.roll_no = roll_no
self.grade = grade
def display (self):
print("Roll no : ", self.roll_no, ", Grade: ", self.grade)
print(stud.__doc__)
- __main__
- Exception is thrown
- Nothing is displayed
- Base class for all students
Ans: Base class for all students
18. What is the output for the following piece of code?
class Demo:
def __init__(self):
self.a = 1
self.__b = 1
def get(self):
return self.__b
obj = Demo()
obj.a=45
print(obj.a)
- The program runs properly and prints 1
- The program has an error because the value of members of a class can’t be changed from outside the class
- The program runs properly and prints 45
- The program has an error because the value of members outside a class can only be changed as self.a=45
Ans: The program runs properly and prints 45
Statement: Mr. Prasanna has his own serviced apprtment given rent for daily basis. Guests can book the rooms online through www.booking.com. In order to maintain the guest register, Prasanna wants an application which gives him the booking details.Booking details include booking id, guest name, room no and the price. Write a Python code to take the details of booking and display the same.Create a class “Booking” with the following attributes. Initialize the constructor using __init__ and use display method to display the details. Data type Attribute Integer bookingid String guestname String roomno Integer price.
Answer the below questions by considering the above statement.
19. What is the name of the class?
- BookingDetails
- Bookings
- Main
- Booking
Ans: Booking
20. How many attributes does the class have?_____________
Ans: 4
21. Choose the right way to define the attributes inside the init method?
- def __init__(self,a,b,c,d) self.bookingid=a self.guestname=b self.roomno=c self.price=d
- def __init__(a,b,c,d): self.bookingid=a self.guestname=b self.roomno=c self.price=d
- def __init__(self,a,b,c,d): self.bookingid=a self.guestname=b self.roomno=c self.price=d
- def __init__(self,a,b,c,d): bookingid=a guestname=b roomno=c price=d
Ans: def init(self,a,b,c,d): self.bookingid=a self.guestname=b self.roomno=c self.price=d
22. How many methods does the class have?
- 1
- 4
- 2
- 3
Ans: 2
23. What is the name of the method present inside the class?
- display
- bookingdetails
- booking
- guestname
Ans: display
24. How many arguments does the method ‘display’ have?
- 0
- 1
- 2
- 3
Ans: 0
25. Choose the correct ‘display’ method definition?
- def display(self) print”Booking ID: “,bookingid print”Guest Name: “,guestname print”Room Number: “,roomno print”Price of the Room: %s Rs”%price
- def display(self): print”Booking ID: “,self.bookingid print”Guest Name: “,self.guestname print”Room Number: “,self.roomno print”Price of the Room: %s Rs”%self.price
- def display(self): print”Booking ID: “,bookingid print”Guest Name: “,guestname print”Room Number: “,roomno print”Price of the Room: %s Rs”%price
- def display(): print”Booking ID: “,bookingid print”Guest Name: “,guestname print”Room Number: “,roomno print”Price of the Room: %s Rs”%price
Ans: def display(self): print”Booking ID: “,self.bookingid print”Guest Name: “,self.guestname print”Room Number: “,self.roomno print”Price of the Room: %s Rs”%self.price
26. How to instantiate the class?
- self.b=Booking(2018051001,”Yashas Chandra”,”R5″,2400)
- b=Booking(2018051001,”Yashas Chandra”,”R5″,2400)
- b=Booking(self, 2018051001,”Yashas Chandra”,”R5″,2400)
- Booking b=Booking(2018051001,”Yashas Chandra”,”R5″,2400)
Ans: b=Booking(2018051001,”Yashas Chandra”,”R5″,2400)
27. How to call the method present inside the class?
- b.display()
- Booking.display()
- display()
- bookingid.display()
Ans: b.display()
28. How to store multiple guests’ Booking details?
- tuple
- list
- dictionary
- all of the above
Ans: all of the above
Happy Learning – If you require any further information, feel free to contact me.