GATE Computer ScienceVery hardGATE CS (PYQ pattern)
#define SQ(x) x * x #define MAX(a, b) ((a) > (b) ? (a) : (b)) int main() { int i = 6, j = 5, k = 6; int r = MAX(i++, j); int s = MAX(j++, i); int t = SQ(k - 1) / SQ(2); printf("%d %d %d %d %d", r, s, t, i, j); } What is printed?
- A. 6 7 6 7 6
- B. 7 8 6 8 6
- C. 6 7 0 7 6
- D. 7 8 0 8 6
Correct answer: D. 7 8 0 8 6
r: (i++) > (j) compares 6 > 5 (i becomes 7), so the chosen branch evaluates i++ again: r = 7, i = 8 (?: has a sequence point after its condition, so this is well defined). s: j++ gives 5 (j becomes 6), 5 > 8 is false, so s = i = 8. t expands to k - 1 * k - 1 / 2 * 2 = 6 - 6 - 0 * 2 = 0, not 25 / 4 = 6. Output 7 8 0 8 6.
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 = -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?
- 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…