ProgrammingVery hardProduct company OA
What is printed? (recursion where the recursive call's result is deliberately ignored)
What is printed? (recursion where the recursive call's result is deliberately ignored)
int calls = 0;
int f(int n) {
calls++;
if (n <= 0) return 0;
f(n - 1);
return calls;
}
int main(void) {
printf("%d", f(4));
}- A. 4
- B. 5
- C. 10
- D. 1
Correct answer: B. 5
f is called for n=4,3,2,1,0 — five invocations total, each incrementing the global calls once (even though most of their return values are discarded by the caller). By the time the outermost call returns, calls is 5.
More C Programming questions
- What is printed? (recursion depth vs. the array being modified on the way back up) c int a[5] = {1, 2, 3, 4, 5}; void f(int i) { if (i >=…
- What is printed? (recursion with an early return skipping the rest of the function) c int f(int n) { if (n < 0) return -1; if (n == 0)…
- What is printed? (binary string of n using recursion, division by 2) c void toBinary(int n) { if (n == 0) return; toBinary(n / 2)…
- What is printed? (two mutually exclusive recursive branches selected by a condition, with accumulation via return value) c int f(int n) {…
- What is printed? (recursion where the base case is reached via an off-by-one condition) c int f(int n) { if (n < 1) return 0; return n +…
- What is printed? (recursion with a pointer parameter used only for read, not accumulation) c int maxOf(int a[], int n) { if (n == 1)…