Python Questions and Answers – Classes and Objects – 2

This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Classes and Objects – 2”.

1. The assignment of more than one function to a particular operator is _______
a) Operator over-assignment
b) Operator overriding
c) Operator overloading
d) Operator instance
View Answer

Answer: c
Explanation: The assignment of more than one function to a particular operator is called as operator overloading.

2. Which of the following is not a class method?
a) Non-static
b) Static
c) Bounded
d) Unbounded
View Answer

Answer: a
Explanation: The three different class methods in Python are static, bounded and unbounded methods.

3. What will be the output of the following Python code?

advertisement
advertisement
def add(c,k):
    c.test=c.test+1
    k=k+1
class A:
    def __init__(self):
        self.test = 0
def main():
    Count=A()
    k=0
 
    for i in range(0,25):
        add(Count,k)
    print("Count.test=", Count.test)
    print("k =", k)
main()

a) Exception is thrown
b)

Count.test=25
k=25
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

c)

Count.test=25
k=0

d)

Count.test=0
k=0
View Answer
Answer: c
Explanation: The program has no error. Here, test is a member of the class while k isn’t. Hence test keeps getting incremented 25 time while k remains 0.
 
 

advertisement

4. Which of the following Python code creates an empty class?
a)

class A:
    return

b)

advertisement
class A:
    pass

c)

class A:

d) It is not possible to create an empty class
View Answer

Answer: b
Explanation: Execute in python shell to verify.

5. Is the following Python code valid?

class B(object):
  def first(self):
    print("First method called")
  def second():
    print("Second method called")
ob = B()
B.first(ob)

a) It isn’t as the object declaration isn’t right
b) It isn’t as there isn’t any __init__ method for initializing class members
c) Yes, this method of calling is called unbounded method call
d) Yes, this method of calling is called bounded method call
View Answer

Answer: c
Explanation: The method may be created in the method demonstrated in the code as well and this is called as the unbounded method call. Calling the method using obj.one() is the bounded method call.

6. What are the methods which begin and end with two underscore characters called?
a) Special methods
b) In-built methods
c) User-defined methods
d) Additional methods
View Answer

Answer: a
Explanation: Special methods like __init__ begin and end with two underscore characters.

7. Special methods need to be explicitly called during object creation.
a) True
b) False
View Answer

Answer: b
Explanation: Special methods are automatically called during object creation.

8. What will be the output of the following Python 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)

a) Error
b) Nothing is printed
c) __str__ called
d) __repr__ called
View Answer

Answer: c
Explanation: __str__ is used for producing a string representation of an object’s value that Python can evaluate. Execute in python shell to verify.

9. What will be the output of the following Python 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)

a) __str__ called
b) __repr__ called
c) Error
d) Nothing is printed
View Answer

Answer: a
Explanation: __str__ is used for producing a string representation of an object’s value that is most readable for humans. Execute in python shell to verify.

10. What is hasattr(obj,name) used for?
a) To access the attribute of the object
b) To delete an attribute
c) To check if an attribute exists or not
d) To set an attribute
View Answer

Answer: c
Explanation: hasattr(obj,name) checks if an attribute exists or not and returns True or False.

11. What will be the output of the following Python code?

class stud:
   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)
stud1 = stud(34, 'S')
stud1.age=7
print(hasattr(stud1, 'age'))

a) Error as age isn’t defined
b) True
c) False
d) 7
View Answer

Answer: b
Explanation: Execute in python shell to verify.

12. What is delattr(obj,name) used for?
a) To print deleted attribute
b) To delete an attribute
c) To check if an attribute is deleted or not
d) To set an attribute
View Answer

Answer: b
Explanation: delattr(obj,name) deletes an attribute in a class.

13. __del__ method is used to destroy instances of a class.
a) True
b) False
View Answer

Answer: a
Explanation: ___del__ method acts as a destructor and is used to destroy objects of classes.

14. What will be the output of the following Python 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(student.__doc__)

a) Exception is thrown
b) __main__
c) Nothing is displayed
d) Base class for all students
View Answer

Answer: d
Explanation: ___doc__ built-in class attribute is used to print the class documentation string or none, if undefined.

15. What does print(Test.__name__) display (assuming Test is the name of the class)?
a) ()
b) Exception is thrown
c) Test
d) __main__
View Answer

Answer: c
Explanation: __name__ built-in class attribute is used to display the class name.

Sanfoundry Global Education & Learning Series – Python.

To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.