ProgrammingHardWipro · Cognizant pattern

What is printed?

What is printed?
int main(void) {
    int a = 1, b = 2, c = 3, d = 4;
    int result = (a < b) ? ((c < d) ? 1 : 2) : ((c < d) ? 3 : 4);
    printf("%d", result);
}
  1. A. 4
  2. B. 1
  3. C. 2
  4. D. 3

Correct answer: B. 1

a<b (1<2) is true, so evaluate the true-branch: (c<d)?1:2. c<d (3<4) is true, giving 1. The false-branch is never evaluated at all.

More C Programming questions

Exams that ask this topic