ProgrammingVery hardProduct company OA

What is printed?

What is printed?
int i = 1, j = 2, k = 3;
int r = (i > j) ? i++ : j > k ? j++ : (k++, i + j + k);
printf("%d %d %d %d", i, j, k, r);
  1. A. 1 3 3 2
  2. B. 1 2 4 6
  3. C. 1 2 3 6
  4. D. 1 2 4 7

Correct answer: D. 1 2 4 7

?: groups right to left, so the else-branch is the whole second conditional. Both tests fail, so only (k++, i + j + k) runs; the comma operator finishes k++ before the sum, giving 1 + 2 + 4 = 7. Only the chosen branch is evaluated, so i and j are untouched. '6' uses the old k.

More C Programming questions

Exams that ask this topic