250+ TOP MCQs on Python Classes and Objects and Answers

Python Multiple Choice Questions 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
Answer: c
Clarification: 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
Answer: a
Clarification: The three different class methods in Python are static, bounded and unbounded methods.

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

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

c)

Count.test=25
k=0

d)

Count.test=0
k=0

View Answer

Answer: c
Clarification: 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.

 
 

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

b)

c)

d) It is not possible to create an empty class
Answer: b
Clarification: 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
Answer: c
Clarification: 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
Answer: a
Clarification: 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
Answer: b
Clarification: 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
Answer: c
Clarification: __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
Answer: a
Clarification: __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
Answer: c
Clarification: 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
Answer: b
Clarification: 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
Answer: b
Clarification: delattr(obj,name) deletes an attribute in a class.

13. __del__ method is used to destroy instances of a class.
a) True
b) False
Answer: a
Clarification: ___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
Answer: d
Clarification: ___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__
Answer: c
Clarification: __name__ built-in class attribute is used to display the class name.

Leave a Reply

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