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)); }- A. 8
- B. 32
- C. 21
- 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
- On a two's-complement machine, what is printed? c int a = 5; printf("%d %d %d", !a, ~a & 0xF, !!a + !~-1);
- What is printed? c const char s = "a1b22c333"; int sum = 0, cur = 0; for (; s; s++) { if (s >= '0' && s <= '9') cur = cur 10 + (s - '0')…
- What is printed? c typedef int (Op)(int); int twice(int x) { return 2 x; } int plus3(int x) { return x + 3; } int apply(Op f, Op g, int x)…
- What is printed? c for (int k = 0; k < 3; k++) { static int s[3]; int a[3] = {0}; s[k] = a[k] = k + 1; printf("%d%d ", s[0] + s[1] + s[2]…
- Assume ASCII. What is printed? c const char s = "pointer"; printf("%c%c%c", s + 1, s[s - 'o' + 2], (s + strlen(s) - 1) - 1);
- With int a = 3, b = 7; which of these statements compile as C (not C++)? c (a > b ? &a : &b) = 0; / (i) / (a > b ? a : b) = 0; / (ii) / a…