Recursion

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


123
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.
There are 25 questions to complete.
123


My Personal Notes arrow_drop_up