ProgrammingHardCognizant GenC · TCS NQT
What is printed?
What is printed?
class Box:
def __init__(self, n):
self.n = n
def __len__(self):
return self.n
def __bool__(self):
return True
print(bool(Box(0)), bool([]), bool(Box(3)), len(Box(4)))- A. False False True 4
- B. True False True 0
- C. True True True 4
- D. True False True 4
Correct answer: D. True False True 4
bool() uses __bool__ before __len__, so a Box of length 0 is still truthy. An empty list is falsy, and len() uses __len__.
More Python Programming questions
- What is printed? python calls = 0 def expensive(): global calls calls += 1 return 0 d = {'x': 5} for k in ['x', 'y', 'x']: v = d.get(k…
- What is printed? python d = {'a': 1, 'b': 2, 'c': 3} try: for k in d: if d[k] == 2: del d[k] except RuntimeError: print('error')…
- What is printed? python a = [1, 2, 2, 3, 4] for x in a: if x == 2: a.remove(x) print(a)
- What is printed? python it = iter(range(7)) print(list(zip(it, it)), list(it))
- What is printed? python print(sum([0.1] 10) == 1.0, sum([0.1] 10), round(sum([0.1] 10), 2))
- What is printed? python x = (5) y = (5,) print(type(x).name, type(y).name, len(y), ((5),) == (5,))