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)); }- A. 1 1
- B. 0 1
- C. 0 0
- 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
- What is printed? c char w[] = {"pear", "fig", "apple", "kiwi"}; for (int i = 0; i < 3; i++) for (int j = 0; j < 3 - i; j++) if…
- What is printed? c int score = 73; char g = score >= 90 ? 'A' : score >= 75 ? 'B' : score >= 60 ? 'C' : 'D'; printf("%c%d", g, g - 'A');
- Which of these statements about C (not C++) are true? (i) A const int variable at file scope is a constant expression, so it can size a…
- Which of these is NOT allowed for a variable declared with the register storage class in C?
- What does p(5) print? c void p(int n) { if (n <= 0) return; p(n - 2); printf("%d", n); p(n - 3); }
- Which statement about this C declaration is true? c char a[5] = "hello";