Top MCQs on Recursion Algorithm with Answers

Last Updated : 27 Sep, 2023

Recursion : The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called as recursive function…. Read More

Recursion Algorithms

Recursion Algorithms


Question 1

Predict output of following program 

C

#include <stdio.h>

int fun(int n)
{
    if (n == 4)
       return n;
    else return 2*fun(n+1);
}


int main()
{
   printf("%d", fun(2));
   return 0;
}

Cross

4

Cross

8

Tick

16

Cross

Runtime Error



Question 1-Explanation: 

Fun(2) = 2 * Fun(3) and Fun(3) = 2 * Fun(4) ....(i)
Fun(4) = 4 ......(ii)
From equation (i) and (ii),
Fun(2) = 2 * 2 * Fun(4)
Fun(2) = 2 * 2 * 4
Fun(2) = 16. 

So, C is the correct answer

Question 2

Consider the following recursive function fun(x, y). What is the value of fun(4, 3) 

C

int fun(int x, int y) 
{
  if (x == 0)
    return y;
  return fun(x - 1,  x + y);
} 
Tick

13

Cross

12

Cross

9

Cross

10



Question 2-Explanation: 

The function fun() calculates and returns ((1 + 2 … + x-1 + x) +y) which is x(x+1)/2 + y.

Hence Option (A) is the correct option.

Question 3

What does the following function print for n = 25? 

C

void fun(int n)
{
  if (n == 0)
    return;

  printf("%d", n%2);
  fun(n/2);
}  
Cross

11001

Tick

10011

Cross

11111

Cross

00000



Question 3-Explanation: 

The function mainly prints binary representation in reverse order.  
And the binary representation of 25: 11001
its reverse is = 10011.

Hence (B) is the correct answer.

Question 4

What does the following function do? 

C

int fun(int x, int y)
{
    if (y == 0)   return 0;
    return (x + fun(x, y-1));
}
Cross

x + y

Cross

x + x*y

Tick

x*y

Cross

xy



Question 4-Explanation: 

int fun(int x, int y): This function is a recursive function that calculates the product of two integers x and y. It does this by repeatedly adding x to itself y times. If y is 0, the function returns 0 (base case). Otherwise, it returns x plus the result of calling itself with x and y-1.

Therefore, The function adds x to itself y times which is x*y.

Hence (C) is the correct answer.

Question 5

What does fun2() do in general? 

C

int fun(int x, int y)
{
    if (y == 0)   return 0;
    return (x + fun(x, y-1));
}

int fun2(int a, int b)
{
    if (b == 0) return 1;
    return fun(a, fun2(a, b-1));
}
Cross

x*y

Cross

x+x*y

Tick

xy

Cross

yx



Question 5-Explanation: 

int fun(int x, int y): This function is a recursive function that calculates the product of two integers x and y. It does this by repeatedly adding x to itself y times. If y is 0, the function returns 0 (base case). Otherwise, it returns x plus the result of calling itself with x and y-1.

int fun2(int a, int b): This function is also a recursive function that calculates the exponentiation of an integer a raised to the power of another integer b. It does this by repeatedly calling itself with the first argument a and decreasing b by 1 until b becomes 0 (base case).

Therefore, fun2() calculates the exponentiation of the first argument a raised to the power of the second argument b. It does this using the fun() function to perform multiplication in a recursive manner.

Hence (C) is the correct answer.

Question 6

Output of following program? 

C

#include<stdio.h>
void print(int n)
{
    if (n > 4000)
        return;
    printf("%d ", n);
    print(2*n);
    printf("%d ", n);
}

int main()
{
    print(1000);
    getchar();
    return 0;
}
Cross

1000 2000 4000

Tick

1000 2000 4000 4000 2000 1000

Cross

1000 2000 4000 2000 1000

Cross

1000 2000 2000 1000



Question 6-Explanation: 

First time n=1000 Then 1000 is printed by first printf function then call print(2*1000) then again print 2000 by printf function then call print(2*2000) and it prints 4000 next time print(4000*2) is called. Here 8000 is greater than 4000 condition becomes true and it return at function(2*4000). Here n=4000 then 4000 will again print through second printf. Similarly print(2*2000) after that n=2000 then 2000 will print and come back at print(2*1000) here n=1000, so print 1000 through second print. 

Hence Option (B) is correct.

Question 7

What does the following function do? 

C

int fun(unsigned int n)
{
    if (n == 0 || n == 1)
        return n;

    if (n%3 != 0)
        return 0;

    return fun(n/3);
}
Cross

It returns 1 when n is a multiple of 3, otherwise returns 0

Tick

It returns 1 when n is a power of 3, otherwise returns 0

Cross

It returns 0 when n is a multiple of 3, otherwise returns 1

Cross

It returns 0 when n is a power of 3, otherwise returns 1



Question 7-Explanation: 

Lets solve with example, n = 27 which power of 3. First time if condition is false as n is neither equal to 0 nor equal to 1 then 27%3 = 0. Here, again if condition false because it is equal to 0. Then fun(27/3) will call. Second time if condition is false as n is neither equal to 0 nor equal to 1 then 9%3 = 0. Here again if condition false because it is equal to 0. Then fun (9/3) will call and third time if condition is false as n is neither equal to 0 nor equal to 1 then 3%3 = 0. Here again if condition false because it is equal to 0. Then fun(3/3) will call here n==1 if condition gets true and it return n i.e. 1. 

Hence option (B) is correct.

Question 8

Predict the output of following program 

C

#include <stdio.h>
int f(int n)
{
    if(n <= 1)
        return 1;
    if(n%2 == 0)
        return f(n/2);
    return f(n/2) + f(n/2+1);
}


int main()
{
    printf("%d", f(11));
    return 0;
}
Cross

Stack Overflow

Cross

3

Cross

4

Tick

5



Question 8-Explanation: 

On successive recursion F(11) will be decomposed into F(5) + F(6) -> F(2) + F(3) + F(3) -> F(1) + 2 * [F(1) + F(2)] -> 1 + 2 * [1 + F(1)] -> 1 + 2 * (1 + 1) -> 5. 

Hence, option D is the correct answer.

Question 9

Consider the following recursive C++ function that takes two arguments 

C++

 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 foo(345, 10)?

Cross

345

Tick

12

Cross

5

Cross

3



Question 9-Explanation: 

The call foo(345, 10) returns the sum of decimal digits (because r is 10) in the number n. The sum of digits for 345 is 3 + 4 + 5 = 12. 

Hence (B) is the correct answer.

Question 10

Consider the same recursive C++ function that takes two arguments 

C++

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(513, 2)?

Cross

9

Cross

8

Cross

5

Tick

2



Question 10-Explanation: 

foo(513, 2) will return 1 + foo(256, 2). All subsequent recursive calls (including foo(256, 2)) will return 0 + foo(n/2, 2) except the last call foo(1, 2) . The last call foo(1, 2) returns 1. So, the value returned by foo(513, 2) is 1 + 0 + 0…. + 0 + 1. The function foo(n, 2) basically returns sum of bits (or count of set bits) in the number n.

Hence (D) is the correct Answer.

There are 30 questions to complete.


Share your thoughts in the comments

Similar Reads