• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests

Top MCQs on Recursion Algorithm with Answers

Question 11

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)?

  • 9

  • 8

  • 5

  • 2

Question 12

Predict the Output:

Java
class GFG
{
  static int f(int a[],int i, int n)
  {
    if(n <= 0) return 0;
    else if(a[i] % 2 == 0) return a[i] + f(a, i+1, n-1);
    else return a[i] - f(a, i+1, n-1);
  }
   public static void main(String args[])
    {
      int a[] = {12, 7, 13, 4, 11, 6};
      System.out.print(f(a,0,6));
    }
}
  • -9

  • 5

  • 15

  • 19

Question 13

Output of following program? 

C
#include <stdio.h>
int fun(int n, int *f_p)
{
    int t, f;
    if (n <= 1)
    {
        *f_p = 1;
        return 1;
    }
    t = fun(n- 1,f_p);
    f = t+ * f_p;
    *f_p = t;
    return f;
}

int main()
{
    int x = 15;
    printf (\" %d \\n\", fun(5, &x));
    return 0;
}
  • 6

  • 8

  • 14

  • 15

Question 14

Consider the JAVA function given below. 

Java
static  int f(int j){
  int i = 50;
  int k;
  if (i == j){
    System.out.print("something");
    k = f(i);
    return 0;
  }
  else return 0;
}

Which one of the following is TRUE?

  • The function returns 0 for all values of j.

  • The function prints the string something for all values of j.

  • The function returns 0 when j = 50.

  • The function will exhaust the runtime stack or run into an infinite loop when j = 50

Question 15

Consider the following C function: 

C
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

  • 5

  • 6

  • 7

  • 8

Question 16

Consider the following C function. 

C
int fun (int n){
  int x = 1, k;
  if (n == 1) 
    return x;
  for (k = 1; k < n; ++k)
     x = x + fun(k) * fun(n  k);
  return x;
}

The return value of fun(5) is __________.

  • 0

  • 26

  • 51

  • 71

Question 17

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

Java
static void get (int n){
  if (n < 1) 
    return;
  get(n - 1);
  get(n - 3);
  System.out.print(n);
}
  • 15

  • 25

  • 35

  • 45

Question 18

What will be the output of the following JAVA program? 

Java
class GFG
{
   static int d=1;
   static void count(int n)
    {
    System.out.print(n+" ");
    System.out.print(d+" ");
    d++;
    if(n > 1) count(n-1);
    System.out.print(d+" ");
  }

 public static void main(String args[])
    {
       count(3);
    }
}
  • 3 1 2 2 1 3 4 4 4

  • 3 1 2 1 1 1 2 2 2

  • 3 1 2 2 1 3 4

  • 3 1 2 1 1 1 2

Question 19

Consider the code fragment written in C below : 

C
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 

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

P2 

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

  • P2 only

  • P1 only

  • Neither P1 nor P2

Question 20

In general, in a recursive and non-recursive implementation of a problem (program) :

  • Both time and space complexities are better in recursive than in non-recursive program.

  • Both time and space complexities are better in non-recursive than in recursive program

  • Time complexity is better in recursive version but space complexity is better in non-recursive version of the program.

  • Space complexity is better in recursive version but time complexity is better in non-recursive version of the program.

There are 30 questions to complete.

Last Updated :
Take a part in the ongoing discussion