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
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; }
4 | |
8 | |
16 | |
Runtime Error |
Discuss it
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)
int fun(int x, int y) { if (x == 0) return y; return fun(x - 1, x + y); }
13 | |
12 | |
9 | |
10 |
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); }
11001 | |
10011 | |
11111 | |
00000 |
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)); }
x + y | |
x + x*y | |
x*y | |
xy |
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)); }
x*y | |
x+x*y | |
xy | |
yx |
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; }
1000 2000 4000 | |
1000 2000 4000 4000 2000 1000 | |
1000 2000 4000 2000 1000 | |
1000 2000 2000 1000 |
Discuss it
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 40000 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 printf.
Option (B) is correct.
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); }
It returns 1 when n is a multiple of 3, otherwise returns 0 | |
It returns 1 when n is a power of 3, otherwise returns 0 | |
It returns 0 when n is a multiple of 3, otherwise returns 1 | |
It returns 0 when n is a power of 3, otherwise returns 1 |
Discuss it
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.
Option (B) is correct.
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; }
Stack Overflow | |
3 | |
4 | |
5 |
Discuss it
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 i.e, 5.
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) ?
345 | |
12 | |
5 | |
3 |
Discuss it
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)?
9 | |
8 | |
5 | |
2 |
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 26 questions to complete.