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?

  1. A. 6 7 6 7 6
  2. B. 7 8 6 8 6
  3. C. 6 7 0 7 6
  4. 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.

More Programming and Data Structures (GATE) questions

Exams that ask this topic