Python MCQs on Overloading

Python MCQs on Overloading

In this section, we are going to introduce you to the most frequently asked Python MCQs on Overloading. These Python MCQs on Overloading will be going to help you in Interviews and Exams.

 

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

f = foo()

format(f)

a) format()

b) __format__()

c) str()

d) __str__()

Check Answer

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

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

a = foo(2)

b = foo(3)

print(a < b)

a)

class foo:

def __init__(self, x):

self.x = x

def __lt__(self, other):

if self.x < other.x:

return False

else:

return True

b)

class foo:

def __init__(self, x):

self.x = x

def __less__(self, other):

if self.x > other.x:

return False

else:

return True

c)

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

Check Answer

Answer: c
__lt__ overloads the < operator>.

3. Which function overloads the + operator?

a) __add__()

b) __plus__()

c) __sum__()

d) none of the mentioned

Check Answer

Answer: a
Refer documentation.

4. Which operator is overloaded by __invert__()?

a) !

b) ~

c) ^

d) –

Check Answer

Answer: b
__invert__() overloads ~.

5. Which function overloads the == operator?

a) __eq__()

b) __equ__()

c) __isequal__()

d) none of the mentioned

Check Answer

Answer: a
The other two do not exist.

6. Which operator is overloaded by __lg__()?

a) <

b) >

c) !=

d) none of the mentioned

Check Answer

Answer: d
__lg__() is invalid.

7. Which function overloads the >> operator?

a) __more__()

b) __gt__()

c) __ge__()

d) none of the mentioned

Check Answer

Answer: d
__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__()

Check Answer

Answer: a
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) /

Check Answer

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

10. Which function overloads the // operator?

a) __div__()

b) __ceildiv__()

c) __floordiv__()

d) __truediv__()

Check Answer

Answer: c
__floordiv__() is for //.

 

Python Interview Questions (MCQs)

We have divided these Python Questions and Answers into various parts based on the topics. Open the Topic of your choice and Practice these MCQs.

Python MCQs on Variables and Operators Python MCQs on Precedence and Associativity
Python MCQs on Data Type Python MCQs on Boolean
Python MCQs on Bitwise Operators Python MCQs on Formatting and Advance Formatting
Python MCQs on Decorators Python MCQs on While and For Loops
Python MCQs on List Python MCQs on List Comprehension
Python MCQs on String Python MCQs on Tuple
Python MCQs on SET Python MCQs on Dictionary
Python MCQs on Functions Python MCQs on Argument Parsing
Python MCQs on Global and Local Variables Python MCQs on Recursion
Python MCQs on Mapping Functions Python MCQs on Modules
Python MCQs on Regular Expressions Python MCQs on Files
Python MCQs on Overloading Python MCQs on Classes and Objects
Python MCQs on Inheritance Python MCQs on Polymorphism and Encapsulation
Python MCQs on Exception Handling

 

Leave a Comment