ProgrammingVery hardProduct company OA · GATE CS

What is printed?

What is printed?
int cnt(int n, int last) {
    if (n == 0) return 1;
    return cnt(n - 1, 0) + (last ? 0 : cnt(n - 1, 1));
}
int main(void) { printf("%d", cnt(5, 0)); }
  1. A. 8
  2. B. 32
  3. C. 21
  4. D. 13

Correct answer: D. 13

cnt counts binary strings of length n with no two adjacent 1s (a 1 may follow only a 0). That count is Fibonacci: 2, 3, 5, 8, 13 for n = 1 … 5. '32' ignores the restriction; 8 and 21 are off-by-one Fibonacci terms.

More C Programming questions

Exams that ask this topic