GATE Computer ScienceHardGATE CS (PYQ pattern)
int a = -17, b = 5; unsigned u = 5; /* 32-bit int and unsigned, C99 or later */ printf("%d %d %u", a / b, a % -b, a / u); What is printed?
- A. -3 -2 858993455
- B. -4 3 858993455
- C. -3 -2 4294967293
- D. -3 2 858993455
Correct answer: A. -3 -2 858993455
Since C99 integer division truncates toward zero: -17 / 5 = -3, and a % b takes the sign of the dividend: -17 % -5 = -2 (so that (a / -b) * -b + a % -b = 15 - 17 = -2 checks). In a / u the int is converted to unsigned: 4294967279 / 5 = 858993455, not (unsigned)(-3) = 4294967293.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- int a = 0, b = 0, c = 0; int r = (a++ && b++) || (++c && a++) || (b++ || ++c); printf("%d %d %d %d", r, a, b, c); What is printed?
- unsigned f(unsigned x) { unsigned c = 0; while (x) { x &= x - 1; c++; } return c; } unsigned g(unsigned x) { return x & ~(x - 1); }…
- int i = 2, j = 3, k, m; k = i += j, j -= i, i j; m = (i, j); printf("%d %d %d %d", i, j, k, m); What is printed?
- int a[] = {10, 20, 30, 40}; int p[] = {a, a + 1, a + 2, a + 3}; int pp = p; int t1 = pp++; int t2 = ++pp; int t3 = ++++pp; printf("%d %d…
- Assume char is 1 byte, int is 4 bytes with 4-byte alignment, double is 8 bytes with 8-byte alignment, and the compiler inserts only the…
- int cnt = 0; int f(int n) { cnt++; if (n <= 1) return 1; if (n == 2) return f(1) + 1; return f(n - 1) + f(n - 2) + f(n - 3); } After f(6)…