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


Question 1
Predict output of following program
#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;
}

A
4
B
8
C
16
D
Runtime Error
Recursion    
Discuss it


Question 1 Explanation: 
Fun(2)
|
2*fun(3)
|
2*fun(4)
|
4

After tree evaluation we get 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)
int fun(int x, int y) 
{
  if (x == 0)
    return y;
  return fun(x - 1,  x + y);
} 
A
13
B
12
C
9
D
10
Recursion    
Discuss it


Question 2 Explanation: 
The function fun() calculates and returns ((1 + 2 … + x-1 + x) +y) which is x(x+1)/2 + y.
Question 3
What does the following function print for n = 25?
void fun(int n)
{
  if (n == 0)
    return;

  printf("%d", n%2);
  fun(n/2);
}  
A
11001
B
10011
C
11111
D
00000
Recursion    
Discuss it


Question 3 Explanation: 
The function mainly prints binary representation in reverse order.
Question 4
What does the following function do?
int fun(int x, int y)
{
    if (y == 0)   return 0;
    return (x + fun(x, y-1));
}
A
x + y
B
x + x*y
C
x*y
D
xy
Recursion    
Discuss it


Question 4 Explanation: 
The function adds x to itself y times which is x*y.
Question 5
What does fun2() do in general?
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));
}
A
x*y
B
x+x*y
C
xy
D
yx
Recursion    
Discuss it


Question 5 Explanation: 
The function multiplies x to itself y times which is xy.
Question 6
Output of following program?
#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;
}
A
1000 2000 4000
B
1000 2000 4000 4000 2000 1000
C
1000 2000 4000 2000 1000
D
1000 2000 2000 1000
Recursion    
Discuss it


Question 7
What does the following function do?
int fun(unsigned int n)
{
    if (n == 0 || n == 1)
        return n;

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

    return fun(n/3);
}
A
It returns 1 when n is a multiple of 3, otherwise returns 0
B
It returns 1 when n is a power of 3, otherwise returns 0
C
It returns 0 when n is a multiple of 3, otherwise returns 1
D
It returns 0 when n is a power of 3, otherwise returns 1
Recursion    
Discuss it


Question 8
Predict the output of following program
#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;
}
A
Stack Overflow
B
3
C
4
D
5
Recursion    
Discuss it


Question 9
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
Recursion    GATE CS 2011    
Discuss it


Question 9 Explanation: 
The call foo(345, 10) returns sum of decimal digits (because r is 10) in the number n. Sum of digits for 345 is 3 + 4 + 5 = 12. pranjul_43 This solution is contributed by Pranjul Ahuja
Question 10
Consider the same 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(513, 2)?
A
9
B
8
C
5
D
2
Recursion    GATE CS 2011    
Discuss it


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.
Question 11
#include<stdio.h>
int f(int *a, int n)
{
  if(n <= 0) return 0;
  else if(*a % 2 == 0) return *a + f(a+1, n-1);
  else return *a - f(a+1, n-1);
}
 
int main()
{
  int a[] = {12, 7, 13, 4, 11, 6};
  printf("%d", f(a, 6));
  getchar();
  return 0;
}
A
-9
B
5
C
15
D
19
Recursion    GATE CS 2010    
Discuss it


Question 11 Explanation: 
f() is a recursive function which adds f(a+1, n-1) to *a if *a is even. If *a is odd then f() subtracts f(a+1, n-1) from *a. See below recursion tree for execution of f(a, 6). .
 f(add(12), 6) /*Since 12 is first element. a contains address of 12 */
    |
    |
 12 + f(add(7), 5) /* Since 7 is the next element, a+1 contains address of 7 */
        |
        |
     7 - f(add(13), 4)
              |
              |
           13 - f(add(4), 3)
                     |
                     |
                  4 + f(add(11), 2)
                             |
                             |
                           11 - f(add(6), 1)
                                    |
                                    |
                                 6 + 0
So, the final returned value is 12 + (7 – (13 – (4 + (11 – (6 + 0))))) = 15
Question 12
Output of following program?
#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;
}
A
6
B
8
C
14
D
15
Recursion    GATE-CS-2009    
Discuss it


Question 12 Explanation: 
Let x is stored at location 2468 i.e. &x = 2468

(dots are use just to ensure alignment)
x = 15
fun(5, 2468)
...{t = fun(4, 2468)
.......{t = fun(3, 2468)
...........{t = fun(2,2468)
...............{t = fun(1, 2468)
...................{// x=1
........................return 1}
................t = 1
................f = 2 //1+1 //since *f_p is x
................x = t = 1
................return 2}
...........t = 2
...........f = 2+1
...........x = t = 2
...........return 3}
........t = 3
........f = 3+2
........x = t = 3
........return 5}
....t = 5
....f = 5+3
....x = t = 5
....return 8}

which implies fun (5,2468) is 8.
Question 13
Consider the following function
double f(double x){
  if (abs(x*x - 3) < 0.01) return x;
  else return f(x/2 + 1.5/x);
}
Give a value q (to 2 decimals) such that f(q) will return q:_____.
A
1.73
B
2.24
C
4.22
D
3.42
Recursion    GATE-CS-2014-(Set-2)    
Discuss it


Question 13 Explanation: 
anil_ds_50 anil_ds_50_1
This solution is contributed by Anil Saikrishna Devarasetty
Question 14
Consider the C function given below.
int f(int j)
{
  static int i = 50;
  int k;
  if (i == j)
  {
    printf(“something”);
    k = f(i);
    return 0;
  }
  else return 0;
}
Which one of the following is TRUE?
A
The function returns 0 for all values of j.
B
The function prints the string something for all values of j.
C
The function returns 0 when j = 50.
D
The function will exhaust the runtime stack or run into an infinite loop when j = 50
Recursion    GATE-CS-2014-(Set-2)    
Discuss it


Question 14 Explanation: 
When j is 50, the function would call itself again and again as neither i nor j is changed inside the recursion.
Question 15
#include<stdio.h>
void crazy(int n,int a,int b)
{
    if (n <= 0)  return;
    crazy(n-1, a, b+n);
    printf("%d %d %d\n",n,a,b);
    crazy(n-1, b, a+n);
}

int main()
{
    crazy(3,4,5);
    return 0;
}
A
1 4 10
2 4 8
1 8 6
3 4 5
1 5 9
2 5 7
1 7 7
B
3 4 5
1 4 10
2 4 8
1 8 6
1 5 9
2 5 7
1 7 7
C
1 4 10
2 4 8
1 8 6
3 4 5
D
3 4 5
1 5 9
2 5 7
1 7 7
Recursion    
Discuss it


Question 16
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
Recursion    GATE-CS-2004    
Discuss it


Question 16 Explanation: 
Question 17
Consider the following C function.
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 __________.
A
0
B
26
C
51
D
71
Recursion    GATE-CS-2015 (Set 2)    
Discuss it


Question 17 Explanation: 
fun(5) = 1 + fun(1) * fun(4) + fun(2) * fun(3) + 
             fun(3) * fun(2) + fun(4) * fun(1)
       = 1 + 2*[fun(1)*fun(4) + fun(2)*fun(3)]

Substituting fun(1) = 1
       = 1 + 2*[fun(4) + fun(2)*fun(3)]

Calculating fun(2), fun(3) and fun(4)
fun(2) = 1 + fun(1)*fun(1) = 1 + 1*1 = 2
fun(3) = 1 + 2*fun(1)*fun(2) = 1 + 2*1*2 = 5
fun(4) = 1 + 2*fun(1)*fun(3) + fun(2)*fun(2)
       = 1 + 2*1*5 + 2*2 = 15

Substituting values of fun(2), fun(3) and fun(4)
fun(5) = 1 + 2*[15 + 2*5] = 51 
Question 18
Consider the following recursive C 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()?
void get (int n)
{
   if (n < 1) return;
   get(n-1);
   get(n-3);
   printf("%d", n);
}
A
15
B
25
C
35
D
45
Recursion    GATE-CS-2015 (Set 3)    
Discuss it


Question 18 Explanation: 
                              get(6) [25 Calls]
                              /      \
               [17 Calls] get(5)       get(3) [7 Calls]
                        /     \
                    get(4)    get(2)[5 Calls]
                   /    \ 
     [7 Calls] get(3)  get(1)[3 Calls]
                /     \
             get(2)   get(0)
            /    \
[3 Calls]get(1)  get(-1) 
   /  \
get(0) get(-2)
We can verify the same by running below program. 1 # include int count = 0; void get (int n) { count++; if (n < 1) return; get(n-1); get(n-3); } int main() { get(6); printf("%d ", count); } [/sourcecode] Output: 25
Question 19
What will be the output of the following C program?
void count(int n)
{
    static int d = 1;
    printf("%d ", n);
    printf("%d ", d);
    d++;
    if(n > 1) count(n-1);
    printf("%d ", d);
}
int main()
{
    count(3);
}
A
3 1 2 2 1 3 4 4 4
B
3 1 2 1 1 1 2 2 2
C
3 1 2 2 1 3 4
D
3 1 2 1 1 1 2
Recursion    GATE-CS-2016 (Set 1)    
Discuss it


Question 19 Explanation: 
count(3) will print value of n and d. So 3 1 will be printed 
and d will become 2. 

Then count(2) will be called. It will print value of n and d. 
So 2 2 will be printed and d will become 3. 

Then count(1) will be called. It will print value of n and d.
So 1 3 will be printed and d will become 4. 

Now count(1) will print value of d which is 4. count(1) will 
finish its execution. 

Then count(2) will print value of d which is 4. 

Similarly, count(3) will print value of d which is 4. 
So series will be A.
Question 20
The function f is defined as follows:
int f (int n) {
    if (n <= 1) return 1;
    else if (n % 2  ==  0) return f(n/2);
    else return f(3n - 1);
}
Assuming that arbitrarily large integers can be passed as a parameter to the function, consider the following statements.
1. The function f terminates for finitely many different values of n ≥ 1.
ii. The function f terminates for infinitely many different values of n ≥ 1.
iii. The function f does not terminate for finitely many different values of n ≥ 1.
iv. The function f does not terminate for infinitely many different values of n ≥ 1.
Which one of the following options is true of the above?
A
(i) and (iii)
B
(i) and (iv)
C
(ii) and (iii)
D
(ii) and (iv)
Recursion    Functions    C Quiz - 113    Gate IT 2007    
Discuss it


Question 20 Explanation: 
The function terminates for all values having a factor of 2 {(2.x)2==0}
So, (i) is false and (ii) is TRUE.
Let n = 3, it will terminate in 2nd iteration.
Let n=5, it will go like 5 - 14 - 7 - 20 - 10 - 5 – and now it will repeat.
And any number with a factor of 5 and 2, there are infinite recursions possible.
So, (iv) is TRUE and (iii) is false.
Question 21
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);
  }
}
What does f(173) print?
A
010110101
B
010101101
C
10110101
D
10101101
Recursion    C Quiz - 112    Gate IT 2008    
Discuss it


Question 21 Explanation: 
(173)2 = 10101101
Question 22
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
Recursion    C Quiz - 112    Gate IT 2008    
Discuss it


Question 22 Explanation: 
  Here, basically the function f prints the binary representation of the number. function f1 also prints the binary representation of the number function f2 prints the binary representation but in reverse order. Output of f  is:- 10101101 Output of f1 is:- 10101101 Output of f2 is:- 10110101 So, the answer is option (C) which is P1 only. This solution is contributed  by Anil Saikrishna Devarasetty.
There are 22 questions to complete.

GATE CS Corner


See Placement Course for placement preparation, GATE Corner for GATE CS Preparation and Quiz Corner for all Quizzes on GeeksQuiz.