Basic Recursion MCQs: 12 Solved C Questions with Step-by-Step Explanations

Solve 12 C recursion questions with exact call traces. Learn to follow descent and unwinding, count invocations, and track parameters and static state.

KnowledgeGate Team

Exam prep & CS education

Updated 23 Aug 20268 min read

Recursive C questions hinge on three checks: the base case, changing arguments, and whether work happens before the call or during unwinding. Miss the third and f(173) prints 10110101 instead of 10101101, from code that looks almost identical. Marks are lost on terminations that never arrive, output printed in the wrong order, counting only one of two recursive calls, static state that survives a return, a sum that main never sees, and a call whose result becomes another call's argument. Choose an option before reading each explanation.

Basic recursion MCQs: base cases and termination

Q1. Choosing the factorial base case

Exam: Bihar STET, Computer Science, 2025.

Which of the following is the correct base case for a recursive function that computes the factorial of a non-negative integer n, so that the recursion terminates correctly for every valid input (including n = 0)?

  • (a) n == 0

  • (b) n == 1

  • (c) n < 0

  • (d) n > 0

Answer: (a) n == 0. Solved page

For factorial(4), the calls are 4 -> 3 -> 2 -> 1 -> 0, and the returns are 1 -> 1 -> 2 -> 6 -> 24. The base case n == 0 stops the chain and handles direct input n = 0.

Q2. Finding a base case that is never reached

Exam: GATE, Computer Science, 2019.

Consider the following C function.

void convert(int n){
    if(n<0)
        printf("%d",n);
    else {
        convert(n/2);
        printf("%d",n%2);
    }
}

Which one of the following will happen when the function convert is called with any positive integer n as argument?

  • (a) It will print the binary representation of n and terminate

  • (b) It will print the binary representation of n in the reverse order and terminate

  • (c) It will print the binary representation of n but will not terminate

  • (d) It will not print anything and will not terminate

Answer: (d) It will not print anything and will not terminate. Solved page

For n = 5, the calls are convert(5) -> convert(2) -> convert(1) -> convert(0) -> convert(0) -> ... because integer 0 / 2 remains 0, so n < 0 is never true. Every printf follows its recursive call; nothing prints before stack overflow.

Recursion call order MCQs: descent, unwinding and binary output

Q3. Tracing output during stack unwinding

Exam: GATE, Computer Science, 2008.

Consider the code fragment below :

#include <stdio.h>

void f(int n) {
    if (n <= 1) {
        printf("%d", n);
    } else {
        f(n / 2);
        printf("%d", n % 2);
    }
}

What does f(173) print?

  • (a) 010110101

  • (b) 010101101

  • (c) 10110101

  • (d) 10101101

Answer: (d) 10101101. Solved page

The descent is f(173) -> f(86) -> f(43) -> f(21) -> f(10) -> f(5) -> f(2) -> f(1). The base prints 1; unwinding prints remainders 0, 1, 0, 1, 1, 0, 1, giving 10101101.

Q4. Comparing print-before and print-after recursion

Exam: GATE, Computer Science, 2008.

Consider the code fragment written in C below:

void f(int n)
{
    if (n <= 1) {
        printf("%d", n);
    }
    else {
        f(n / 2);
        printf("%d", n % 2);
    }
}

Which of the following implementations will produce the same output for f(173) as the above code?

P1

void f(int n)
{
    if (n / 2) {
        f(n / 2);
    }
    printf("%d", n % 2);
}

P2

void f(int n)
{
    if (n <= 1) {
        printf("%d", n);
    }
    else {
        printf("%d", n % 2);
        f(n / 2);
    }
}
  • (a) Both P1 and P2

  • (b) P2 only

  • (c) P1 only

  • (d) Neither P1 nor P2

Answer: (c) P1 only. Solved page

P1 reaches the same base value and prints each n % 2 during unwinding, producing 10101101. P2 prints during descent and produces 10110101; only P1 is equivalent.

Call-stack trace for f(173): descent to f(1), then unwinding prints 10101101, with P1 matching and P2 printing 10110101.

Recursion return-value MCQs: accumulating answers as calls unwind

Q5. Adding digits through recursive returns

Exam: GATE, Computer Science, 2011.

Consider the following recursive C function that takes two arguments.

unsigned int foo(unsigned int n, unsigned int r) {
    if (n>0) return ((n%r) + foo(n/r, r));
    else return 0;
}

What is the return value of the function foo when it is called as foo(345, 10)?

  • (a) 345

  • (b) 12

  • (c) 5

  • (d) 3

Answer: (b) 12. Solved page

Expand the returns: foo(345,10) = 5 + foo(34,10) = 5 + 4 + foo(3,10) = 5 + 4 + 3 + foo(0,10) = 12. Division by 10 removes each digit; % 10 adds it.

Q6. Tracking two changing arguments

Exam: GATE, Computer Science, 2005.

What is the output printed by the following program?

#include<stdio.h>
int f(int n, int k)
{
    if (n == 0)
        return 0;
    else if (n % 2)
        return f(n/2, 2*k) + k;
    else return f(n/2, 2*k) - k;
}
int main ()
{
    printf("%d", f(20, 1));
    return 0;
}
  • (a) 5

  • (b) 8

  • (c) 9

  • (d) 20

Answer: (c) 9. Solved page

Trace both arguments: f(20,1) = f(10,2) - 1, f(10,2) = f(5,4) - 2, f(5,4) = f(2,8) + 4, f(2,8) = f(1,16) - 8, and f(1,16) = f(0,32) + 16 = 16. Unwinding gives f(2,8)=8, f(5,4)=12, f(10,2)=10, and f(20,1)=9.

Counting recursive calls: invocations and repeated phases

Q7. Counting every recursive invocation

Exam: GATE, Computer Science, 2015.

Consider the following recursive C function.

void get(int n)
{
    if (n<1) return;
    get (n-1);
    get (n-3);
    printf("%d", n);
}

If get(6) function is being called in main()  then how many times will the get()  function be invoked before returning to the main()?

  • (a) 15

  • (b) 25

  • (c) 35

  • (d) 45

Answer: (b) 25. Solved page

Base calls count, so use T(n)=1 for n<1 and T(n)=1+T(n-1)+T(n-3) for n>=1. This gives T(1)=3, T(2)=5, T(3)=7, T(4)=11, T(5)=17, and T(6)=25.

Q8. Counting repeated recursive phases

Exam: UGC NET, Computer Science, 2024.

Consider the function in C code:

void Cal(int a, int b)
{
    if (b != 1)
    {
        if (a != 1)
        {
            printf("*");
            Cal(a / 2, b);
        }
        else
        {
            b = b - 1;
            Cal(10, b);
        }
    }
}

How many times * is going to be printed, if the function is called with Cal(10, 10); ?

  • (a) 25

  • (b) 23

  • (c) 24

  • (d) 27

Answer: (d) 27. Solved page

For each fixed b, a: 10 -> 5 -> 2 -> 1 prints three stars before resetting a and reducing b. The nine phases use b = 10, 9, 8, 7, 6, 5, 4, 3, 2, so 9 x 3 = 27 stars.

Recurrence ladder for get(6) giving T(6)=25, beside the Cal(10,10) grid of nine phases printing 27 stars in total.

Static state and pass-by-value in recursive C functions

Q9. Following a static variable across calls

Exam: ISRO, Computer Science, 2008.

Consider the following C function:

int f(int n)
{
static int i = 1;
if(n >= 5) return n;
n = n+i;
i++;
return f(n);
}

The value returned by f(1) is

  • (a) 5

  • (b) 6

  • (c) 7

  • (d) 8

Answer: (c) 7. Solved page

Trace (n,i): (1,1) -> (2,2) -> (4,3) -> (7,4). At (7,4), n >= 5, so the function returns 7. The static i is shared across calls.

Q10. Separating a local copy from the caller's value

Exam: GATE, Computer Science, 2005.

Consider the following C-program:

void foo(int n, int sum)
{
  int k = 0, j = 0;
  if (n == 0) return;
  k = n % 10;
  j = n / 10;
  sum = sum + k;
  foo (j, sum);
  printf ("%d,", k);
}

int main ()
{
  int a = 2048, sum = 0;
  foo (a, sum);
  printf ("%d\n", sum);

  getchar();
}

What does the above program print?

  • (a) 8, 4, 0, 2, 14

  • (b) 8, 4, 0, 2, 0

  • (c) 2, 0, 4, 8, 14

  • (d) 2, 0, 4, 8, 0

Answer: (d) 2, 0, 4, 8, 0. Solved page

After each addition, (n,k,sum) is (2048,8,8), (204,4,12), (20,0,12), then (2,2,14), followed by n=0. Unwinding prints 2,0,4,8,; because sum is passed by value, main retains 0. The output is 2, 0, 4, 8, 0.

Nested recursive calls and recognising a standard algorithm

Q11. Resolving nested recursion

Exam: GATE, Computer Science, 1998.

What value would the following function return for the input x = 95?

Function fun (x: integer): integer;
Begin
    If x > 100 then fun := x - 10
    Else fun := fun(fun(x + 11))
End;
  • (a) 89

  • (b) 90

  • (c) 91

  • (d) 92

Answer: (c) 91. Solved page

The listing is Pascal-style pseudocode; written in C the same function is int fun(int x){ return x > 100 ? x - 10 : fun(fun(x + 11)); }. One recursive result becomes another call's argument: fun(95)=fun(fun(106))=fun(96). The pattern advances through fun(97) to fun(100), where fun(100)=fun(fun(111))=fun(101)=91; all pending calls return 91.

Q12. Recognising Euclid's algorithm

Exam: Coal India, Computer Science, 2020.

Consider the following recursive function.

int function (int x, int y) {
    if (y <= 0) return x;
    return function (y, x % y);
}

The above recursive function computes ______.

  • (a) GCD of x and y

  • (b) yˣ

  • (c) x × y

  • (d) LCM of x and y

Answer: (a) GCD of x and y. Solved page

The update (x,y) -> (y,x%y) preserves the GCD while reducing the second argument. The trace function(48,18) -> function(18,12) -> function(12,6) -> function(6,0) -> 6 identifies Euclid's GCD algorithm.

Basic recursion MCQ patterns and the next practice step

Before solving, check termination, mark descent versus unwinding work, count base calls, and label state as local, parameter, global, or static.

KnowledgeGate carries more than 60 practice questions on basic recursion in C. For broader objective practice, continue with Data Structures MCQs, then apply the same call-stack reasoning to recursive traversals in Binary Trees and Binary Search Trees. Dynamic Programming Explained: 0/1 Knapsack shows the next idea: storing repeated recursive subproblems instead of recomputing them.

Reattempt Q2, Q6, Q7, Q9, and Q10 without looking at the answers. Your written checkpoints should be 0 / 2 = 0, f(20,1)=9, T(6)=25, (1,1)->(2,2)->(4,3)->(7,4), and 2,0,4,8,0.

For a structured route through functions and recursion, use the C Programming Course. To compare it with wider placement-focused practice, browse Coding & DSA Courses for Placements, then return to these five checkpoints until you can rebuild every trace on paper.