ProgrammingVery hardJEE Advanced · CAT placement

What is printed?

What is printed?
print(sum([1e100, 1.0, -1e100]) == 1.0, sum([1, 1e100, -1e100]), 1e100 + 1.0 - 1e100)
  1. A. False 0 0.0
  2. B. True 0.0 0.0
  3. C. True 1.0 0.0
  4. D. False 1.0 0.0

Correct answer: B. True 0.0 0.0

Plain left-to-right addition loses the 1.0 next to 1e100. Since Python 3.12, sum() of floats compensates for rounding, so sum([1e100, 1.0, -1e100]) keeps 1.0. But the integer 1 is absorbed when it is added before the first float, so sum([1, 1e100, -1e100]) is 0.0.

More Python Programming questions

Exams that ask this topic