Data Structures & AlgorithmsHardAmazon · Microsoft OA

Edge case: chained comparisons and booleans as integers in Python.

Edge case: chained comparisons and booleans as integers in Python.
print(1 < 2 > 1.5, (1 < 2) > 1.5, True + True, 3 > 2 == 2)
What is printed?
  1. A. True False 2 True
  2. B. True True 2 False
  3. C. False False 2 True
  4. D. True False True True

Correct answer: A. True False 2 True

1 < 2 > 1.5 means (1 < 2) and (2 > 1.5): True. (1 < 2) > 1.5 compares True (= 1) with 1.5: False. bool is a subclass of int, so True + True = 2. 3 > 2 == 2 means (3 > 2) and (2 == 2): True.

More Coding Interview Problems questions

Exams that ask this topic