Python Questions and Answers – Operator Overloading

This set of Python Interview Questions and Answers for Experienced people focuses on “Operator Overloading”

1. Which function is called when the following Python code is executed?

f = foo()
format(f)

a) format()
b) __format__()
c) str()
d) __str__()
View Answer

Answer: d
Explanation: Both str(f) and format(f) call f.__str__().
advertisement
advertisement

2. Which of the following Python code will print True?

a = foo(2)
b = foo(3)
print(a < b)

a)

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
class foo:
    def __init__(self, x):
        self.x = x
    def __lt__(self, other):
        if self.x < other.x:
            return False
        else:
            return True

b)

advertisement
class foo:
    def __init__(self, x):
        self.x = x
    def __less__(self, other):
        if self.x > other.x:
            return False
        else:
            return True

c)

advertisement
class foo:
    def __init__(self, x):
        self.x = x
    def __lt__(self, other):
        if self.x < other.x:
            return True
        else:
            return False

d)

class foo:
    def __init__(self, x):
        self.x = x
    def __less__(self, other):
        if self.x < other.x:
            return False
        else:
            return True
View Answer
Answer: c
Explanation: __lt__ overloads the < operator>.
 
 

3. Which function overloads the + operator?
a) __add__()
b) __plus__()
c) __sum__()
d) none of the mentioned
View Answer

Answer: a
Explanation: Refer documentation.

4. Which operator is overloaded by __invert__()?
a) !
b) ~
c) ^
d) –
View Answer

Answer: b
Explanation: __invert__() overloads ~.

5. Which function overloads the == operator?
a) __eq__()
b) __equ__()
c) __isequal__()
d) none of the mentioned
View Answer

Answer: a
Explanation: The other two do not exist.

6. Which operator is overloaded by __lg__()?
a) <
b) >
c) !=
d) none of the mentioned
View Answer

Answer: d
Explanation: __lg__() is invalid.

7. Which function overloads the >> operator?
a) __more__()
b) __gt__()
c) __ge__()
d) none of the mentioned
View Answer

Answer: d
Explanation: __rshift__() overloads the >> operator.

8. Let A and B be objects of class Foo. Which functions are called when print(A + B) is executed?
a) __add__(), __str__()
b) __str__(), __add__()
c) __sum__(), __str__()
d) __str__(), __sum__()
View Answer

Answer: a
Explanation: The function __add__() is called first since it is within the bracket. The function __str__() is then called on the object that we received after adding A and B.

9. Which operator is overloaded by the __or__() function?
a) ||
b) |
c) //
d) /
View Answer

Answer: b
Explanation: The function __or__() overloads the bitwise OR operator |.

10. Which function overloads the // operator?
a) __div__()
b) __ceildiv__()
c) __floordiv__()
d) __truediv__()
View Answer

Answer: c
Explanation: __floordiv__() is for //.

Sanfoundry Global Education & Learning Series – Python.

To practice all interview questions on Python for experienced people, here is complete set of 1000+ Multiple Choice Questions and Answers.

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.