30+ MCQs on Python Special Methods(Dunder Methods)

Ayushi Trivedi Last Updated : 06 Mar, 2024
7 min read

Welcome to the Python Special Methods Python Interview Questions! Special methods, also known as “magic” or “dunder” methods, are prefixed and suffixed with double underscores in Python. These methods provide functionality to classes that enable them to emulate built-in types or operations. These questions will test your understanding of various special methods in Python, including methods for object representation, arithmetic operations, comparison, and more. Each question is multiple-choice, with only one correct answer. Take your time to carefully read each question and choose the best option. Let’s explore the world of Python special methods together!

Python Special Methods

30+ Python Interview Questions on Python Special Methods

Q1. What do double underscore (__) prefixed methods in Python signify?

a) They are reserved for internal use and should not be called directly.

b) They are special methods known as “dunder” methods.

c) They are called magic methods and handle specific operations.

d) All of the above

Answer: d

Explanation: Double underscore (__) prefixed methods are special methods in Python known as “dunder” methods or magic methods. They are reserved for internal use and handle specific operations.

Q2. What is the purpose of the __init__ method in a Python class?

a) To initialize the class object with default attributes.

b) To create a new instance of the class.

c) To define the constructor of the class.

d) To destroy the class object.

Answer: c

Explanation: The __init__ method is the constructor in Python classes. It is used to initialize the object’s attributes when a new instance of the class is created.

Q3. Which dunder method is used to implement the behavior of the + operator in Python?

a) __add__

b) __plus__

c) __sum__

d) __concat__

Answer: a

Explanation: The __add__ method is used to define the behavior of the + operator in Python classes.

Q4. Which of the following methods is used to represent an object as a string in Python?

a) __repr__

b) __str__

c) __display__

d) __format__

Answer: b

Explanation: The __str__ method is used to define how an object should be represented as a string when using str(object) or print(object).

Q5. What does the __len__ method do in Python?

a) It returns the length of the object.

b) It defines the behavior of the length operator (len()).

c) It checks if an object is empty.

d) It returns the number of elements in the object.

Answer: a

Explanation: The __len__ method is used to define the behavior of the length operator (len()) for custom objects, returning the length of the object.

Q6. Which dunder method is used to implement the behavior of the * operator in Python?

a) __mult__

b) __mul__

c) __product__

d) __times__

Answer: b

Explanation: The __mul__ method is used to define the behavior of the * operator in Python classes.

Q7. What is the purpose of the __getitem__ method in Python?

a) It is used to get the value of an item from a dictionary.

b) It is used to define the behavior for accessing items using square brackets ([]).

c) It is used to retrieve an item from a list.

d) It is used to get the item at a specific index in a sequence.

Answer: b

Explanation: The __getitem__ method is used to define the behavior for accessing items using square brackets ([]) for custom objects.

Q8. Which dunder method is used to implement the behavior of the in operator in Python?

a) __exists__

b) __contains__

c) __includes__

d) __in__

Answer: b

Explanation: The __contains__ method is used to define the behavior of the in operator in Python classes.

Q9. What is the purpose of the __setattr__ method in Python?

a) It is used to set the value of an attribute.

b) It is used to define the behavior of attribute assignment.

c) It is used to check if an attribute exists.

d) It is used to delete an attribute.

Answer: b

Explanation: The __setattr__ method is used to define the behavior when an attribute is set in a Python class.

Q10. Which dunder method is used to implement the behavior of the - operator in Python?

a) __sub__

b) __minus__

c) __subtract__

d) __difference__

Answer: a

Explanation: The __sub__ method is used to define the behavior of the - operator in Python classes.

Q11. What does the __iter__ method do in Python?

a) It returns an iterator object for the class.

b) It checks if an object is iterable.

c) It defines the behavior of the for loop for the class.

d) It returns the next item in the iteration.

Answer: a

Explanation: The __iter__ method is used to return an iterator object for the class, allowing it to be used in a for loop.

Q12. Which dunder method is used to implement the behavior of the // operator in Python?

a) __floor_div__

b) __floordiv__

c) __divide__

d) __intdiv__

Answer: b

Explanation: The __floordiv__ method is used to define the behavior of the // operator in Python classes.

Q13. What is the purpose of the __contains__ method in Python?

a) It checks if an element is contained in an object.

b) It checks if an object contains another object.

c) It checks if an attribute is contained in a class.

d) It checks if an element is contained in a sequence.

Answer: d

Explanation: The __contains__ method is used to define the behavior of the in operator for custom objects, checking if an element is contained in a sequence.

Q14. Which dunder method is used to implement the behavior of the == operator in Python?

a) __equal__

b) __eq__

c) __equals__

d) __is__

Answer: b

Explanation: The __eq__ method is used to define the behavior of the == operator in Python classes.

Q15. What does the __add__ method do in Python?

a) It adds two objects together.

b) It concatenates two objects.

c) It performs element-wise addition for two objects.

d) It performs matrix multiplication for two objects.

Answer: a

Explanation: The __add__ method is used to define the behavior of the + operator for custom objects, allowing them to be added together.

Q16. Which dunder method is used to implement the behavior of the [] operator for setting values in Python?

a) __setitem__

b) __set__

c) __assign__

d) __update__

Answer: a

Explanation: The __setitem__ method is used to define the behavior of the [] operator for setting values in Python classes.

Q17. What is the purpose of the __delattr__ method in Python?

a) It is used to delete an attribute from an object.

b) It is used to define the behavior when an attribute is deleted.

c) It is used to check if an attribute can be deleted.

d) It is used to return the value of a deleted attribute.

Answer: a

Explanation: The __delattr__ method is used to define the behavior when an attribute is deleted from a Python object.

Q18. What does the __next__ method do in Python?

a) It returns the next element in an iteration.

b) It checks if there are more elements in an iteration.

c) It defines the behavior of the next() function.

d) It raises a StopIteration exception.

Answer: a

Explanation: The __next__ method is used to define the behavior of retrieving the next element in an iteration.

Q19. Which dunder method is used to implement the behavior of the ** operator in Python?

a) __exp__

b) __pow__

c) __power__

d) __exponent__

Answer: b

Explanation: The __pow__ method is used to define the behavior of the ** operator for exponentiation in Python classes.

Q24. Which dunder method is used to implement the behavior of the != operator in Python?

a) __not_equal__

b) __ne__

c) __noteq__

d) __not_equals__

Answer: b

Explanation: The __ne__ method is used to define the behavior of the != operator in Python classes.

Q25. What is the purpose of the __call__ method in Python?

a) It calls another method in the class.

b) It defines the behavior of calling an instance of the class as a function.

c) It checks if the object is callable.

d) It returns the object’s type.

Answer: b

Explanation: The __call__ method is used to define the behavior of calling an instance of the class as if it were a function.

Q26. Which dunder method is used to implement the behavior of the -= operator in Python?

a) __decr__

b) __minus_equal__

c) __isub__

d) __sub_assign__

Answer: c

Explanation: The __isub__ method is used to define the behavior of the -= operator in Python classes.

Q27. Which dunder method is used to implement the behavior of the += operator in Python?

a) __increase__

b) __add_assign__

c) __iadd__

d) __plus_equal__

Answer: c

Explanation: The __iadd__ method is used to define the behavior of the += operator in Python classes.

Q28. Which dunder method is used to implement the behavior of the > operator in Python?

a) __gt__

b) __greater__

c) __greater_than__

d) __isgreater__

Answer: a

Explanation: The __gt__ method is used to define the behavior of the > operator in Python classes.

Q29. Which dunder method is used to implement the behavior of the <= operator in Python?

a) __le__

b) __less__

c) __lessthan__

d) __islessthan__

Answer: a

Explanation: The __le__ method is used to define the behavior of the <= operator in Python classes.

Q30. Which dunder method is used to implement the behavior of the >= operator in Python?

a) __ge__

b) __greater__

c) __greaterthan__

d) __isgreater__

Answer: a

Explanation: The __ge__ method is used to define the behavior of the >= operator in Python classes.

Q31. Which dunder method is used to implement the behavior of the < operator in Python?

a) __lt__

b) __lesser__

c) __lessthan__

d) __islessthan__

Answer: a

Explanation: The __lt__ method is used to define the behavior of the < operator in Python classes.

Q32. Which special method is used to overload the addition operator + in Python?

class MyClass:
    def __init__(self, value):
        self.value = value
    
    def __add__(self, other):
        return MyClass(self.value + other.value)
    
    def __str__(self):
        return f"MyClass object with value: {self.value}"

obj1 = MyClass(5)
obj2 = MyClass(10)
result = obj1 + obj2
print(result)

a) __add__

b) __plus__

c) __sum__

d) __concat__

Answer: a

Explanation: The __add__ method overloads the addition operator + in Python classes.

Q33. Which special method is used to overload the division operator / in Python for division?

class Fraction:
    def __init__(self, numerator, denominator):
        self.numerator = numerator
        self.denominator = denominator
    
    def __truediv__(self, other):
        return Fraction(self.numerator * other.denominator, self.denominator * other.numerator)
    
    def __str__(self):
        return f"{self.numerator}/{self.denominator}"

f1 = Fraction(1, 2)
f2 = Fraction(3, 4)
result = f1 / f2
print(result)

a) __divide__

b) __truediv__

c) __fraction__

d) __div__

Answer: b

Explanation: The __truediv__ method overloads the division operator / in Python classes.

Q34. Which special method is used to overload the less than operator < in Python?

class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y
    
    def __lt__(self, other):
        return self.x < other.x and self.y < other.y
    
    def __str__(self):
        return f"Point({self.x}, {self.y})"

point1 = Point(5, 10)
point2 = Point(8, 12)
result = point1 < point2
print(result)

a) __less__

b) __lt__

c) __lessthan__

d) __islessthan__

Answer: b

Explanation: The __lt__ method overloads the less than operator < in Python classes.

Congratulations on completing the Python Special Methods MCQs! Special methods in Python provide a powerful way to customize the behavior of objects and classes, making them more versatile and expressive. By mastering these special methods, you gain the ability to define how objects interact with operators, implement custom representations, handle comparisons, and more. Keep practicing and experimenting with Python’s special methods to become proficient in building flexible and powerful Python classes. If you have any questions or want to delve deeper into any topic, don’t hesitate to continue your learning journey. Happy coding!

You can also enroll in our free Python Course Today!

Read our more articles related to MCQs in Python:

My name is Ayushi Trivedi. I am a B. Tech graduate. I have 3 years of experience working as an educator and content editor. I have worked with various python libraries, like numpy, pandas, seaborn, matplotlib, scikit, imblearn, linear regression and many more. I am also an author. My first book named #turning25 has been published and is available on amazon and flipkart. Here, I am technical content editor at Analytics Vidhya. I feel proud and happy to be AVian. I have a great team to work with. I love building the bridge between the technology and the learner.

Responses From Readers

Clear

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details