ProgrammingVery hardProduct company OA

For ints a, b >= 0, which test correctly tells, before adding, whether a + b would overflow?

  1. A. b > INT_MAX / a
  2. B. a + b < 0
  3. C. a > INT_MAX - b
  4. D. a + b > INT_MAX

Correct answer: C. a > INT_MAX - b

INT_MAX − b cannot overflow when b ≥ 0, and a > INT_MAX − b is exactly a + b > INT_MAX rearranged. Computing a + b first already commits signed overflow (undefined), so testing it afterwards is useless; the division test checks multiplication and fails for a = 0.

More C Programming questions

Exams that ask this topic