ProgrammingVery hardProduct company OA · GATE CS
What is printed?
What is printed?
int calls = 0;
int C(int n, int k) {
calls++;
return (k == 0 || k == n) ? 1 : C(n - 1, k - 1) + C(n - 1, k);
}
int main(void) {
int r = C(6, 3);
printf("%d %d", r, calls);
}- A. 15 29
- B. 20 41
- C. 20 39
- D. 20 20
Correct answer: C. 20 39
This is Pascal's rule, so C(6, 3) = 20. The recursion tree is binary with every leaf returning 1, so it has 20 leaves and 19 internal nodes: 39 calls. '20 20' counts only the leaves.
More C Programming questions
- What is printed? c enum E { A = -2, B, C, D = A + 10, E2 }; printf("%d %d %d", B, C, E2);
- What is printed? c int steps(int n) { return n == 1 ? 0 : 1 + steps(n % 2 ? 3 n + 1 : n / 2); } int main(void) { printf("%d %d", steps(6)…
- Assume ASCII. What does f("Code21") print? c void f(const char s) { if (s) { f(s + 1); if (s % 2) putchar(s); } }
- What is printed? c int a = 1, b = 2, c = 3; int arr[] = {&a, &b, &c}; int pp = arr + 1; pp += 10; pp[1] += 20; printf("%d %d %d", a, b, c);
- Assume 32-bit unsigned int. What is printed? c unsigned mask(int lo, int hi) { return ((1u << (hi - lo + 1)) - 1) << lo; } int main(void)…
- What is printed? c int f(unsigned x) { int c = 0; while (x & (x >> 1)) { x &= x >> 1; c++; } return c + (x != 0); } int main(void) {…