Test Series - python

Test Number 102/108

Q: Which function is called when the following Python code is executed?

f = foo()
format(f)
A. format()
B. __format__()
C. str()
D. __str__()
Solution: Both str(f) and format(f) call f.__str__().
Q: 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
Solution: __lt__ overloads the < operator>.
Q: Which function overloads the + operator?
A. __add__()
B. __plus__()
C. __sum__()
D. none of the mentioned
Solution: Refer documentation.
Q: Which operator is overloaded by __invert__()?
A. !
B. ~
C. ^
D. 
Solution:  __invert__() overloads ~.
Q: Which function overloads the == operator?
A. __eq__()
B. __equ__()
C. __isequal__()
D. none of the mentioned
Solution: The other two do not exist.
Q: Which operator is overloaded by __lg__()?
A. <
B. >
C. !=
D. none of the mentioned
Solution: __lg__() is invalid.
Q: Which function overloads the >> operator?
A. __more__()
B. __gt__()
C. __ge__()
D. none of the mentioned
Solution: __rshift__() overloads the >> operator.
Q: 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__()
Solution: 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.
Q: Which operator is overloaded by the __or__() function?
A. ||
B. |
C. //
D. /
Solution: The function __or__() overloads the bitwise OR operator |.
Q: Which function overloads the // operator?
A. __div__()
B. __ceildiv__()
C. __floordiv__()
D. __truediv__()
Solution: __floordiv__() is for //.

You Have Score    /10