ProgrammingVery hardProduct company OA
For ints a, b >= 0, which test correctly tells, before adding, whether a + b would overflow?
- A. b > INT_MAX / a
- B. a + b < 0
- C. a > INT_MAX - b
- 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
- What is printed? c const char s = " hi there you "; int words = 0, in = 0; for (; s; s++) { if (s == ' ') in = 0; else if (!in) { in = 1…
- What is printed? c int sub(const char a, const char b) { / is a a subsequence of b? / if (!a) return 1; if (!b) return 0; return sub(a ==…
- What is printed? c char src[] = "abc", dst[8]; char d = dst; const char s = src; while ((d++ = s++)); printf("%s %td %td", dst, d - dst, s…
- What does calloc(n, size) guarantee that malloc(n size) does not?
- What is printed? c int a[3][3] = {{1}, {2, 3}, [2][1] = 7}; printf("%d %d %d %d", a[0][1], a[1][1], a[2][0], a[2][1]);
- What does f(6) print? c void f(int n) { printf("%d", n); if (n > 1) f(n / 2); printf("%d", n); }