ProgrammingHardWipro · Cognizant pattern
What is printed?
What is printed?
int f(int n) {
if (n <= 0) return 0;
int a = f(n - 1);
int b = f(n - 1);
return a + b + 1;
}
int main(void) {
printf("%d", f(3));
}- A. 3
- B. 15
- C. 8
- D. 7
Correct answer: D. 7
This builds a full binary tree of recursive calls (each level makes TWO separate calls with the same argument, n-1). f(0)=0, f(1)=0+0+1=1, f(2)=1+1+1=3, f(3)=3+3+1=7, following the pattern f(n) = 2^n - 1.
More C Programming questions
- What is printed? c void countdown(int n) { if (n < 0) return; printf("%d ", n); countdown(n - 1); } int main(void) { countdown(3)…
- What is printed? c int f(int n) { if (n <= 0) return 0; (n)--; return 1 + f(n); } int main(void) { int x = 5; printf("%d %d", f(&x), x); }
- Assume a 64-bit LP64 ABI with natural alignment (no #pragma pack). What is printed? c struct S { char a; int b; char c; }; printf("%zu"…
- Assume a 64-bit LP64 ABI with natural alignment. What is printed? c struct S { int a; char b; int c; }; printf("%zu", sizeof(struct S));
- Assume a 64-bit LP64 ABI with #pragma pack(1) applied to this struct only. What is printed? c #pragma pack(push, 1) struct Packed { char…
- Assume a 64-bit LP64 ABI. What is printed? c struct A { char x; }; struct B { struct A a; char y; }; struct C { struct B b; char z; }…