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!
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.
__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.
+
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.
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)
.
__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.
*
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.
__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.
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.
__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.
-
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.
__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.
//
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.
__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.
==
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.
__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.
[]
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.
__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.
__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.
**
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.
!=
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.
__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.
-=
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.
+=
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.
>
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.
<=
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.
>=
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.
<
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.
+
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.
/
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.
<
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: