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)))
  1. A. False False True 4
  2. B. True False True 0
  3. C. True True True 4
  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

Exams that ask this topic