ProgrammingModerateAccenture · Capgemini

What is printed?

What is printed?
int even(int n);
int odd(int n)  { return n == 0 ? 0 : even(n - 1); }
int even(int n) { return n == 0 ? 1 : odd(n - 1); }
int main(void) { printf("%d %d", even(7), odd(7)); }
  1. A. 1 1
  2. B. 0 1
  3. C. 0 0
  4. D. 1 0

Correct answer: B. 0 1

The two functions hand the question back and forth, flipping between 'even' and 'odd' each time n drops by 1. After 7 steps n = 0 is reached in the opposite function to the one you started in, so even(7) = 0 and odd(7) = 1. The prototype for even is what lets odd call it before it is defined.

More C Programming questions

Exams that ask this topic