Recursive Mcq1
Recursive Mcq1
#include <stdio.h>
int fun(int x, int y)
{
if (x == 0)
return y;
else
return fun(x - 1, x + y);
}
int main()
{
int a = fun(5, 2);
printf("%d", a);
return 0;
}
A. 10
B. 15
C. 14
D. 17
ANSWER: D
How many times the function factorial will be executed?
#include <stdio.h>
int factorial(int);
int main()
{
int n = 6, f;
f = factorial(n);
printf("%d! = %ld\n", n, f);
return 0;
}
int factorial(int n)
{
if (n == 0)
return 1;
else
return (n * factorial(n - 1));
}
A. 1
B. 6
C. 7
D. infinite
ANSWER: C
This is an example of recursion. The function ‘factorial’ will be called 7 times to compute the
factorial of 6.
#include <stdio.h>
int f(int n, int k)
{
if (n == 0)
return 0;
else if (n % 2)
return f(n / 2, 2 * k) + k;
else
return f(n / 2, 2 * k) - k;
}
int main()
{
printf("%d", f(20, 1));
return 0;
}
A. 5
B. 8
C. 9
D. 20
ANSWER: C
f(20,1) = 9.
f(10,2) - 1 = 9
f(5,4) - 2 = 10
f(2,8) + 4 = 12
f(1,16) - 8 = 8
f(0,32) + 16 = 16
#include <stdio.h>
int fun(int x)
{
int fun1;
if (x > 100)
fun1 = x - 10;
else
fun1 = fun(fun(x + 11));
return fun1;
}
int main()
{
int y;
y = fun(95);
printf("%d", y);
return 0;
}
A. 95
B. 91
C. 90
D. 106
ANSWER: B
#include <stdio.h>
int main()
{
printf("Hello world\n");
main();
return 0;
}
A. Infinite times
B. 32767
C. 65535
D. Till stack overflows
ANSWER: D
A call stack or function stack is used for several related purposes, but the main reason for having
one is to keep track of the point to which each active subroutine should return control when it
finishes executing. A stack overflow occurs when too much memory is used on the call stack.
Here function main() is called repeatedly and its return address is stored in the stack. After
stack memory is full. It shows stack overflow error.
#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. Error
ANSWER: C
void function()
{
function();
}
int main()
{
function();
return 0;
}
ANSWER: D
Every function call is stored in the stack memory. In this case, there is no terminating condition
(base case). So, function() will be called continuously till the stack overflows and there is no
more space to store the function calls. At this point of time, the program will stop abruptly.
What is the return value of the function foo when it is called as func(240, 7)?
A. 10
B. 12
C. 14
D. 16
ANSWER: B
func(240, 7) will return 2 + func(34, 7). All subsequent recursive calls (including func(34, 7))
will return 6 + func(4, 7) except the last call func(4, 7) . The last call func(4, 7) returns 4. So, the
value returned by func(240, 7) is 2 + 6 + 4=12.
ANSWER: B
#include<stdio.h>
int func(int n, int sum)
{
int k = 0, j = 0;
if (n == 0)
return;
k = n % 10;
j = n / 10;
sum = sum + k;
func(j, sum);
printf("%d,", k);
}
int main() {
int a = 2048, sum = 0;
func(a, sum);
printf("%d ", sum);
return 0;
}
A. 8 ,4, 0, 2, 14
B. 8, 4, 0, 2, 0
C. 2, 0, 4, 8, 14
D. 2, 0, 4, 8, 0
ANSWER: D
sum has no use in func(), it is there just to confuse. Function func() just prints all digits of a
number. In main, there is one more printf statement after func(), so one more 0 is printed after
all digits of n.
int fun2(int n)
{
if (n == 0)
return 0;
fun2(n / 2);
printf("%d", n % 2);
return 0;
}
A. 2.5
B. 101
C. 2.0000
D. 1
ANSWER: B
The function finds the binary equivalent of the number n. Thus, the output is 101.
void function(int n)
{
if (n == 0)
return;
printf("%d ", n);
function(n - 1);
}
int main()
{
function(10);
return 0;
}
A. 10
B. 1
C. 10 9 8 . . . 1
D. 10 9 8….0
ANSWER: C
What is the return value of the function func when it is called as func(513, 2)?
A. 0
B. 2
C. 4
D. 6
E. 8
ANSWER: B
func(513, 2) will return 1 + func(256, 2). All subsequent recursive calls (including func(256, 2))
will return 0 + func(n/2, 2) except the last call func(1, 2) . The last call func(1, 2) returns 1. So,
the value returned by func(513, 2) is 1 + 0 + 0…. + 0 + 1=2.
void func(int n)
{
int i = 0;
if (n > 1)
func(n - 1);
for (i = 0; i < n; i++)
printf(" * ");
}
A. 5
B. 10
C. 15
D. 20
E. 25
ANSWER: C
Solution: The func is called 5 times and each times ‘*’ is printed n number of times. Thus,
n(n+1)/2 i.e. 15 times ‘*’ will be printed.
Explanation:
Yes it’s a runtime error, here the main() function repeatedly called the main() and the program
never ends.
Explanation:
Recursive function will always work as a stack FILO (Fist In Last Out).
How it works
Here fun(--n) is a calling function inside fun(int n)
irrespective of recursive call, the value will be stored in the memory location as shown below
i.e) statement below fun( --n ) will execute for every time when the function is called,
irrespective of recursion above it.
while displaying output, it simply reverse its order as it stores the value in stack.
Explanation:
main(); function is called repeatedly and the program never ends until your system memory
crash.
Explanation:
A call stack or function stack is used for several related purposes, but the main reason for having
one is to keep track of the point to which each active subroutine should return control when it
finishes executing.
A stack overflow occurs when too much memory is used on the call stack.
Here function main() is called repeatedly and its return address is stored in the stack. After
stack memory is full. It shows stack overflow error.
Explanation:
Explanation:
When a recursive call is made, the function/process clones itself and then process that function.
This leads to time and space constrains.
In a loop, there is no recursive call involved that saves a lot of time and space too.