Functions & Recursion Questions with Answers

23 practice questions on Functions & Recursion (C) with answers and step-by-step solutions. Try 10 samples below, then practise all 23 free in the AptiNest app.

Sample questions

Question 1Easy

Which of these features is NOT supported in standard C?

  1. Functions with a variable number of arguments
  2. Recursive functions
  3. Pointers to functions
  4. Function overloading (two functions with the same name but different parameters)

Answer: D. Function overloading (two functions with the same name but different parameters)

Explanation

C requires each function name with external linkage to be unique, so overloading is not allowed (C++ adds it). Recursion, function pointers and variadic functions are all standard C.

Step-by-step solution

  1. Function overloading is when two functions have same name but different parameters.
  2. In C, each function name must be unique, so this is not allowed.
  3. Recursive functions are allowed in C.
  4. Pointers to functions are supported in C.
  5. Functions with variable arguments like printf() are standard in C.
  6. Therefore, function overloading is NOT supported in standard C.

Why the other options are wrong

  • A. Variadic functions are standard in C through stdarg.h and printf, so this feature is supported.
  • B. Recursive functions are allowed in C; each call simply gets its own stack frame.
  • C. Pointers to functions are standard in C, used for callbacks and tables of operations.

Tip: Remember: C has no function overloading.

Question 2Easy

What happens when this code is compiled?

#include <stdio.h>

int sq(int x) { return x * x; }

int main(void) {
    printf("%d\n", sq(2, 3));
    return 0;
}
  1. It prints 9
  2. It prints 6
  3. Compilation error: too many arguments to function call
  4. It prints 4

Answer: C. Compilation error: too many arguments to function call

Explanation

sq has a prototype with one parameter; calling it with two arguments violates a constraint and the compiler must diagnose it.

Step-by-step solution

  1. Function sq is declared to take one integer argument.
  2. sq(2, 3) passes two integers to a function expecting one, which is invalid.
  3. The compiler detects this mismatch and reports an error.
  4. Error message indicates too many arguments for the function call.
  5. Thus, compilation fails with the specified error message.

Why the other options are wrong

  • A. Nine would be 3 * 3; sq squares only its first argument, and the extra argument is rejected.
  • B. Six is not produced; calling sq with two arguments is a compile error, so nothing prints.
  • D. Four is 2 * 2 from the first argument, but the extra argument makes the code fail to compile.

Tip: Always match the number of arguments in function calls with their declarations.

Question 3Easy

What is the output?

#include <stdio.h>

int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a % b);
}

int main(void) {
    printf("%d\n", gcd(48, 18));
    return 0;
}
  1. 2
  2. 18
  3. 3
  4. 6

Answer: D. 6

Explanation

Euclid: gcd(48,18) -> gcd(18,12) -> gcd(12,6) -> gcd(6,0) = 6.

Step-by-step solution

  1. gcd(48, 18) calls gcd(18, 48 % 18 = 12).
  2. gcd(18, 12) calls gcd(12, 18 % 12 = 6).
  3. gcd(12, 6) calls gcd(6, 12 % 6 = 0).
  4. gcd(6, 0) returns 6.
  5. Printf prints 6.

Why the other options are wrong

  • A. Two is a common factor but not the greatest one; the Euclid steps end with 6 for 48 and 18.
  • B. Eighteen is only the second argument; the recursion keeps shrinking until the remainder is 0, returning 6.
  • C. Three divides both numbers but is not the greatest; the recursion returns 6 instead.

Tip: Use Euclid's algorithm for GCD: replace numbers with their remainder until one becomes zero.

Question 4Easy

What is the output?

#include <stdio.h>

void swap(int a, int b) {
    int t = a;
    a = b;
    b = t;
}

int main(void) {
    int x = 1, y = 2;
    swap(x, y);
    printf("%d %d\n", x, y);
    return 0;
}
  1. 2 1
  2. 2 2
  3. 1 2
  4. 1 1

Answer: C. 1 2

Explanation

C passes arguments by value: swap exchanges its own copies a and b, leaving x and y in main unchanged.

Step-by-step solution

  1. Swap function gets copies of x and y (1 and 2).
  2. Inside swap, t = 1, a = 2, b = 1.
  3. A and b are swapped but x and y in main remain unchanged.
  4. Printf prints original values of x and y: 1 2.

Why the other options are wrong

  • A. Swapped values would need pointers; swap gets copies, so x and y in main keep 1 and 2.
  • B. Two twos is wrong; swap changes only its local copies, so main still prints 1 and 2.
  • D. Ones for both is wrong; y is 2 in main and swap never changes it, so 2 is printed.

Tip: Remember: C passes arguments by value, not reference.

Question 5Medium

What does main return if control reaches its closing brace without executing a return statement (C99 and later)?

  1. 0
  2. An indeterminate value
  3. -1
  4. 1

Answer: A. 0

Explanation

Reaching the } that terminates main is equivalent to returning 0. This special rule applies only to main.

Step-by-step solution

  1. In C99 and later, reaching the closing brace of main is the same as executing return 0.
  2. This special rule applies only to main.
  3. So main returns 0, which is option A.

Why the other options are wrong

  • B. An indeterminate value is not what main returns here; the rule gives 0.
  • C. -1 is not the default; main returns 0 when it reaches its closing brace.
  • D. 1 is not the default; main returns 0 when it reaches its closing brace.

Tip: In C99 and later, main without a return statement returns 0; other functions do not get this rule.

Question 6Medium

What is the output?

#include <stdio.h>

int moves = 0;

void hanoi(int n, char from, char to, char via) {
    if (n == 0)
        return;
    hanoi(n - 1, from, via, to);
    moves++;
    hanoi(n - 1, via, to, from);
}

int main(void) {
    hanoi(4, 'A', 'C', 'B');
    printf("%d\n", moves);
    return 0;
}
  1. 8
  2. 15
  3. 16
  4. 7

Answer: B. 15

Explanation

Tower of Hanoi needs M(n) = 2M(n-1) + 1 moves, i.e. 2^n - 1; for 4 discs that is 15.

Step-by-step solution

  1. Moves for n discs: 2^n - 1.
  2. For n = 4: 15.

Why the other options are wrong

  • A. 8 is 2^3.
  • C. 16 is 2^4.
  • D. 7 is for 3 discs.

Tip: 2^n - 1.

Question 7Medium

What is the output?

#include <stdio.h>

int f(int n) {
    if (n == 0)
        return 0;
    return n + f(n / 2);
}

int main(void) {
    printf("%d\n", f(10));
    return 0;
}
  1. 18
  2. 20
  3. 15
  4. 55

Answer: A. 18

Explanation

f(10) = 10 + f(5)

= 10 + 5 + f(2)

= 15 + 2 + f(1)

= 17 + 1 + f(0)

= 18.

Step-by-step solution

  1. f(10) = 10 + f(5), since 10 / 2 = 5.
  2. f(5) = 5 + f(2), and f(2) = 2 + f(1).
  3. f(1) = 1 + f(0) = 1 + 0 = 1.
  4. Total: 10 + 5 + 2 + 1 = 18, which is option A.

Why the other options are wrong

  • B. 20 does not match the calls, which add only 10, 5, 2 and 1.
  • C. 15 stops after 10 + 5 and misses f(2) and f(1).
  • D. 55 would be the sum of 1 to 10; the function halves n, it does not subtract 1.

Tip: Trace a recursive function by writing each call and its return value on one line.

Question 8Medium

What is the output?

#include <stdarg.h>
#include <stdio.h>

int sum(int count, ...) {
    va_list ap;
    va_start(ap, count);
    int s = 0;
    for (int i = 0; i < count; i++)
        s += va_arg(ap, int);
    va_end(ap);
    return s;
}

int main(void) {
    printf("%d\n", sum(3, 4, 5, 6));
    return 0;
}
  1. 15
  2. 18
  3. 9
  4. 3

Answer: A. 15

Explanation

The first argument says three values follow; va_arg reads 4, 5 and 6, so the sum is 15. The count itself is not added.

Step-by-step solution

  1. The first argument, 3, says three variable values follow.
  2. Va_arg reads 4, 5 and 6 in order.
  3. The sum is 4 + 5 + 6 = 15, which is option A.

Why the other options are wrong

  • B. 18 adds the count 3 as if it were a value; the count is not summed.
  • C. 9 is 4 + 5 and misses the value 6.
  • D. 3 is the count alone, not the sum of the values read.

Tip: The first fixed argument tells how many variable values to read; it is not added to the sum.

Question 9Hard

What is the output?

#include <stdio.h>

int f(int n) {
    if (n == 0)
        return 0;
    return n % 2 + 10 * f(n / 2);
}

int main(void) {
    printf("%d\n", f(10));
    return 0;
}
  1. 5
  2. 0101
  3. 10
  4. 1010

Answer: D. 1010

Explanation

The function builds the binary digits of n as a decimal-looking number: f(10) = 0 + 10 × f(5), f(5) = 1 + 10 × f(2) = 1 + 10 × 10 = 101, so f(10) = 1010.

Step-by-step solution

  1. f(10): 10 % 2 = 0, so f(10) = 0 + 10 × f(5).
  2. f(5): 5 % 2 = 1, so f(5) = 1 + 10 × f(2).
  3. f(2) = 0 + 10 × f(1), and f(1) = 1 + 10 × f(0) = 1, so f(2) = 10.
  4. f(5) = 1 + 100 = 101, so f(10) = 10 × 101 = 1010, which is option D.

Why the other options are wrong

  • A. 5 is not the built value; the function builds the digits of 10 in binary, giving 1010.
  • B. 0101 is not what the calls produce; the value is 1010.
  • C. 10 is only f(2), an intermediate value, not f(10).

Tip: Trace recursion down to the base case, then build the result on the way back up.

Question 10Hard

What is the output?

#include <stdio.h>

int calls = 0;

int fib(int n) {
    calls++;
    if (n < 2)
        return n;
    return fib(n - 1) + fib(n - 2);
}

int main(void) {
    fib(5);
    printf("%d\n", calls);
    return 0;
}
  1. 9
  2. 15
  3. 8
  4. 5

Answer: B. 15

Explanation

The call count C(n) = 1 + C(n-1) + C(n-2) with C(0) = C(1) = 1 gives
C(2)=3,
C(3)=5,
C(4)=9,
C(5)=15.

Step-by-step solution

  1. calls(n) = 1 + calls(n-1) + calls(n-2).
  2. calls(0)=calls(1)=1, calls(2)=3, calls(3)=5, calls(4)=9, calls(5)=15.

Why the other options are wrong

  • A. 9 is for fib(4).
  • C. 8 is the fib value, not the calls.
  • D. 5 is for fib(3).

Tip: calls(n) = 2*fib(n+1) - 1.

Practise all 23 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all C topics · All topics