ProgrammingVery hardProduct company OA

What is printed?

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

Correct answer: B. 1 2 6 1

||, &&, the comma operator and ?: all introduce sequence points, so this is fully defined. i++ yields 0 (i = 1), so j++ is evaluated: 5 is true (j = 6). The right side of && runs: k becomes 1 and i + j = 7 > 1. ?: has lower precedence than &&, so the whole && expression is the condition; it is true, r = i++ = 1 and i ends as 2. The third operand (j += 2, k) is never evaluated. Output 1 2 6 1.

More C Programming questions

Exams that ask this topic