ProgrammingHardTCS NQT · Infosys pattern

What is printed?

What is printed?
int x = 7;
int result = x % 2 == 0 ? x / 2 : x * 3 + 1;
printf("%d", result);
  1. A. 10
  2. B. 22
  3. C. 21
  4. D. 3

Correct answer: B. 22

x % 2 == 0 (7%2==0, i.e. 1==0) is false, so the else branch runs: x*3+1 = 21+1 = 22 (this is one step of the Collatz sequence for odd x).

More C Programming questions

Exams that ask this topic