ProgrammingHardTCS NQT · Infosys pattern
What is printed?
What is printed?
#include <stdio.h>
void f(int n) {
if (n <= 0) { printf("B "); return; }
if (n % 2 == 0) printf("E%d ", n);
else printf("O%d ", n);
f(n - 2);
}
int main(void) {
f(5);
}- A. O5 O3 O1 B
- B. O5 O3 O1
- C. E5 E3 E1 B
- D. O5 O3 O1 O-1 B
Correct answer: A. O5 O3 O1 B
n descends 5, 3, 1, -1 in steps of 2. 5, 3 and 1 are all odd, each printing O<n>. When n becomes -1, the base case n<=0 fires immediately, printing B without reaching the odd/even check, giving O5 O3 O1 B.
More C Programming questions
- What is printed? c #include <stdio.h> int digitSum(int n) { if (n == 0) return 0; printf("%d ", n % 10); return n % 10 + digitSum(n / 10)…
- What is printed? c #include <stdio.h> void count(int n) { int local = n; if (n == 0) { printf("local=%d ", local); return; } count(n - 1)…
- What is printed? c #include <stdio.h> int f(int n); int g(int n) { if (n <= 0) return n; return f(n - 2); } int f(int n) { if (n <= 0)…
- What is printed? c #include <stdio.h> int odds = 0; void f(int n) { if (n == 0) return; if (!(n % 2)) { f(n - 1); return; } odds++; f(n…
- What is printed? c int calls = 0; int fib(int n) { calls++; if (n <= 1) return n; return fib(n - 1) + fib(n - 2); } int main(void) { int r…
- What is printed? c int f(int n, int acc) { if (n == 0) return acc; return f(n - 1, acc n); } int main(void) { printf("%d", f(4, 1)); }