ProgrammingHardGATE CS · Campus placements

For unsigned a and b greater than 0 stored in uint32_t, which test correctly detects that a * b overflows?

  1. A. a + b < b
  2. B. b > UINT32_MAX / a
  3. C. a * b < a
  4. D. a > b

Correct answer: B. b > UINT32_MAX / a

If a * b exceeds UINT32_MAX then b > UINT32_MAX / a. The test a * b < a misses cases where the wrapped product stays larger than a, for example a = 3 and b = 2^31 + 1.

More C Programming questions

Exams that ask this topic