ProgrammingModerateWipro · Capgemini pattern

What is printed?

What is printed?
int a = -1, b = 1;
if (a > 0)
    if (b > 0) printf("X");
else printf("Y");
printf("Z");
  1. A. YZ
  2. B. Y
  3. C. Z
  4. D. XZ

Correct answer: C. Z

An else pairs with the nearest unmatched if, whatever the indentation suggests, so else belongs to if (b > 0). Since a > 0 is false, that whole inner if-else is skipped and only Z prints.

More C Programming questions

Exams that ask this topic