ProgrammingHardGATE CS · Campus placements
For unsigned a and b greater than 0 stored in uint32_t, which test correctly detects that a * b overflows?
- A. a + b < b
- B. b > UINT32_MAX / a
- C. a * b < a
- 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
- char words[] holds strings and must be sorted by length first and then alphabetically using qsort. Which comparator is correct?
- char buf[4]; int n = snprintf(buf, sizeof buf, "%d", 12345); What is n?
- uint8t x = 0xB7; uint8t y = x | (x + 1); What is y?
- The number of ways to tile a 2 x 6 board with 2 x 1 dominoes equals the number of binary tilings given by T(n) = T(n-1) + T(n-2) with T(1)…
- Intervals (1,4), (2,3) and (3,5) are stored as pairs in an array and qsort sorts them ascending by their second value. Which pair comes…
- Which of these statements are well defined by the C standard? I. uint8t x = 255; x = x + 1; leaves x equal to 0. II. int a = INTMAX; a + 1…