0% found this document useful (0 votes)
37 views

Unit 1 Recurcive MCQ

The document discusses recursion and provides examples to find the factorial of a number using recursion. It explains that recursion involves defining a problem in terms of smaller instances of the same problem until a base case is reached. It also discusses the time and space complexity of recursive factorial implementations.

Uploaded by

snippet one
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Unit 1 Recurcive MCQ

The document discusses recursion and provides examples to find the factorial of a number using recursion. It explains that recursion involves defining a problem in terms of smaller instances of the same problem until a base case is reached. It also discusses the time and space complexity of recursive factorial implementations.

Uploaded by

snippet one
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Unit – 1

Recursion

1. Recursion is a method in which the solution of a problem depends on ____________


a) Larger instances of different problems
b) Larger instances of the same problem
c) Smaller instances of the same problem
d) Smaller instances of different problems
View Answer
Answer: c
Explanation: In recursion, the solution of a problem depends on the solution of smaller
instances of the same problem.
2. Which of the following problems can’t be solved using recursion?
a) Factorial of a number
b) Nth fibonacci number
c) Length of a string
d) Problems without base case
View Answer

Answer: d
Explanation: Problems without base case leads to infinite recursion call. In general, we will
assume a base case to avoid infinite recursion call. Problems like finding Factorial of a
number, Nth Fibonacci number and Length of a string can be solved using recursion.
3. Recursion is similar to which of the following?
a) Switch Case
b) Loop
c) If-else
d) if elif else
View Answer
Answer: b
Explanation: Recursion is similar to a loop.
4. In recursion, the condition for which the function will stop calling itself is ____________
a) Best case
b) Worst case
c) Base case
d) There is no such condition
View Answer

Answer: c
Explanation: For recursion to end at some point, there always has to be a condition for which
the function will not call itself. This condition is known as base case.
5. What will happen when the below code snippet is executed?
void my_recursive_function()
{
my_recursive_function();
}
int main()
{
my_recursive_function();
return 0;
}
a) The code will be executed successfully and no output will be generated
b) The code will be executed successfully and random output will be generated
c) The code will show a compile time error
d) The code will run for some time and stop when the stack overflows
View Answer

Answer: d
Explanation: Every function call is stored in the stack memory. In this case, there is no
terminating condition(base case). So, my_recursive_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.
6. What is the output of the following code?

void my_recursive_function(int n)
{
if(n == 0)
return;
printf("%d ",n);
my_recursive_function(n-1);
}
int main()
{
my_recursive_function(10);
return 0;
}
a) 10
b) 1
c) 10 9 8 … 1 0
d) 10 9 8 … 1
View Answer

Answer: d
Explanation: The program prints the numbers from 10 to 1.
7. What is the base case for the following code?

void my_recursive_function(int n)
{
if(n == 0)
return;
printf("%d ",n);
my_recursive_function(n-1);
}
int main()
{
my_recursive_function(10);
return 0;
}
a) return
b) printf(“%d “, n)
c) if(n == 0)
d) my_recursive_function(n-1)
View Answer

Answer: c
Explanation: For the base case, the recursive function is not called. So, “if(n == 0)” is the
base case.
8. How many times is the recursive function called, when the following code is executed?

void my_recursive_function(int n)
{
if(n == 0)
return;
printf("%d ",n);
my_recursive_function(n-1);
}
int main()
{
my_recursive_function(10);
return 0;
}
a) 9
b) 10
c) 11
d) 12
View Answer
Answer: c
Explanation: The recursive function is called 11 times.
9. What does the following recursive code do?

void my_recursive_function(int n)
{
if(n == 0)
return;
my_recursive_function(n-1);
printf("%d ",n);
}
int main()
{
my_recursive_function(10);
return 0;
}
a) Prints the numbers from 10 to 1
b) Prints the numbers from 10 to 0
c) Prints the numbers from 1 to 10
d) Prints the numbers from 0 to 10
View Answer

Answer: c
Explanation: The above code prints the numbers from 1 to 10.
10. Which of the following statements is true?
a) Recursion is always better than iteration
b) Recursion uses more memory compared to iteration
c) Recursion uses less memory compared to iteration
d) Iteration is always better and simpler than recursion
View Answer
Answer: b
Explanation: Recursion uses more memory compared to iteration because every time the
recursive function is called, the function call is stored in stack.
11. What will be the output of the following code?

int cnt=0;
void my_recursive_function(int n)
{
if(n == 0)
return;
cnt++;
my_recursive_function(n/10);
}
int main()
{
my_recursive_function(123456789);
printf("%d",cnt);
return 0;
}
a) 123456789
b) 10
c) 0
d) 9
View Answer

Answer: d
Explanation: The program prints the number of digits in the number 123456789, which is 9.
12. What will be the output of the following code?
void my_recursive_function(int n)
{
if(n == 0)
{
printf("False");
return;
}
if(n == 1)
{
printf("True");
return;
}
if(n%2==0)
my_recursive_function(n/2);
else
{
printf("False");
return;
}

}
int main()
{
my_recursive_function(100);
return 0;
}
a) True
b) False
View Answer
Answer: b
Explanation: The function checks if a number is a power of 2. Since 100 is not a power of 2,
it prints false.

13. What is the output of the following code?


int cnt = 0;
void my_recursive_function(char *s, int i)
{
if(s[i] == '\0')
return;
if(s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u')
cnt++;
my_recursive_function(s,i+1);
}
int main()
{
my_recursive_function("thisisrecursion",0);
printf("%d",cnt);
return 0;
}
a) 6
b) 9
c) 5
d) 10
View Answer

Answer: a
Explanation: The function counts the number of vowels in a string. In this case the number is
vowels is 6.
14. What is the output of the following code?
void my_recursive_function(int *arr, int val, int idx, int len)
{
if(idx == len)
{
printf("-1")
return ;
}
if(arr[idx] == val)
{
printf("%d",idx);
return;
}
my_recursive_function(arr,val,idx+1,len);
}
int main()
{
int array[10] = {7, 6, 4, 3, 2, 1, 9, 5, 0, 8};
int value = 2;
int len = 10;
my_recursive_function(array, value, 0, len);
return 0;
}
a) 3
b) 4
c) 5
d) 6
View Answer

Answer: b
Explanation: The program searches for a value in the given array and prints the index at
which the value is found. In this case, the program searches for value = 2. Since, the index of
2 is 4(0 based indexing), the program prints 4.

“Factorial using Recursion”.


1. In general, which of the following methods isn’t used to find the factorial of a number?
a) Recursion
b) Iteration
c) Dynamic programming
d) Non iterative / recursive
View Answer
Answer: d
Explanation: In general we use recursion, iteration and dynamic programming to find the
factorial of a number. We can also implement without using iterative / recursive method by
using tgammal() method. Most of us never use it generally.

2. Which of the following recursive formula can be used to find the factorial of a number?
a) fact(n) = n * fact(n)
b) fact(n) = n * fact(n+1)
c) fact(n) = n * fact(n-1)
d) fact(n) = n * fact(1)
View Answer
Answer: c
Explanation: fact(n) = n * fact(n – 1) can be used to find the factorial of a number.

3. Consider the following iterative implementation to find the factorial of a number. Which of
the lines should be inserted to complete the below code?

int main()
{
int n = 6, i;
int fact = 1;
for(i=1;i<=n;i++)
_________;
printf("%d",fact);
return 0;
}
a) fact = fact + i
b) fact = fact * i
c) i = i * fact
d) i = i + fact
View Answer
Answer: b
Explanation: The line “fact = fact * i” should be inserted to complete the above code.

4. Consider the following recursive implementation to find the factorial of a number. Which
of the lines should be inserted to complete the below code?

int fact(int n)
{
if(_________)
return 1;
return n * fact(n - 1);
}
int main()
{
int n = 5;
int ans = fact(n);
printf("%d",ans);
return 0;
}
a) n = 0
b) n != 0
c) n == 0
d) n == 1
View Answer
Answer: c
Explanation: The line “n == 0” should be inserted to complete the above code.
Note: “n == 1” cannot be used because it does not take care of the case when n = 0, i.e when
we want to find the factorial of 0.

5. The time complexity of the following recursive implementation to find the factorial of a
number is ________

int fact(int n)
{
if(_________)
return 1;
return n * fact(n - 1);
}
int main()
{
int n = 5;
int ans = fact(n);
printf("%d",ans);
return 0;
}
a) O(1)
b) O(n)
c) O(n2)
d) O(n3)
View Answer
Answer: b
Explanation: The time complexity of the above recursive implementation to find the factorial
of a number is O(n).

6. What is the space complexity of the following recursive implementation to find the
factorial of a number?

int fact(int n)
{
if(_________)
return 1;
return n * fact(n - 1);
}
int main()
{
int n = 5;
int ans = fact(n);
printf("%d",ans);
return 0;
}
a) O(1)
b) O(n)
c) O(n2)
d) O(n3)
View Answer
Answer: a
Explanation: The space complexity of the above recursive implementation to find the
factorial of a number is O(1).

7. Consider the following recursive implementation to find the factorial of a number. Which
of the lines is the base case?

int fact(int n)
{
if(n == 0)
return 1;
return n * fact(n - 1);
}
int main()
{
int n = 5;
int ans = fact(n);
printf("%d",ans);
return 0;
}
a) return 1
b) return n * fact(n-1)
c) if(n == 0)
d) if(n == 1)
View Answer
Answer: c
Explanation: The line “if(n == 0)” is the base case.

8. What is the output of the following code?

int fact(int n)
{
if(n == 0)
return 1;
return n * fact(n - 1);
}
int main()
{
int n = 0;
int ans = fact(n);
printf("%d",ans);
return 0;
}
a) 0
b) 1
c) 2
d) 3
View Answer
Answer: b
Explanation: The program prints 0!, which is 1.
9. What is the output of the following code?

int fact(int n)
{
if(n == 0)
return 1;
return n * fact(n - 1);
}
int main()
{
int n = 1;
int ans = fact(n);
printf("%d",ans);
return 0;
}
a) 0
b) 1
c) 2
d) 3
View Answer
Answer: b
Explanation: The program prints 1!, which is 1.

10. How many times will the function fact() be called when the following code is executed?

int fact(int n)
{
if(n == 0)
return 1;
return n * fact(n - 1);
}
int main()
{
int n = 5;
int ans = fact(n);
printf("%d",ans);
return 0;
}
a) 4
b) 5
c) 6
d) 7
View Answer
Answer: c
Explanation: The fact() function will be called 6 times with the following arguments:
fact(5), fact(4), fact(3), fact(2), fact(1), fact(0).
11. What is the output of the following code?

int fact(int n)
{
if(n == 0)
return 1;
return n * fact(n - 1);
}
int main()
{
int n = 5;
int ans = fact(n);
printf("%d",ans);
return 0;
}
a) 24
b) 120
c) 720
d) 1
View Answer
Answer: b
Explanation: The function prints 5!, which is 120.

“Fibonacci using Recursion”.

1. Suppose the first fibonnaci number is 0 and the second is 1. What is the sixth fibonnaci
number?
a) 5
b) 6
c) 7
d) 8
View Answer
Answer: a
Explanation: The sixth fibonnaci number is 5.
2. Which of the following is not a fibonnaci number?
a) 8
b) 21
c) 55
d) 14
View Answer
Answer: d
Explanation: 14 is not a fibonnaci number.
3. Which of the following option is wrong?
a) Fibonacci number can be calculated by using Dynamic programming
b) Fibonacci number can be calculated by using Recursion method
c) Fibonacci number can be calculated by using Iteration method
d) No method is defined to calculate Fibonacci number
View Answer
Answer: d
Explanation: Fibonacci number can be calculated by using Dynamic Programming,
Recursion method, Iteration Method.
advertisement
4. Consider the following iterative implementation to find the nth fibonacci number?

int main()
{
int n = 10,i;
if(n == 1)
printf("0");
else if(n == 2)
printf("1");
else
{
int a = 0, b = 1, c;
for(i = 3; i <= n; i++)
{
c = a + b;
________;
________;
}
printf("%d",c);
}
return 0;
}
Which of the following lines should be added to complete the above code?
a)

c=b

b=a

b)

a=b

b=c

c)

b=c
a=b

d)

a=b

b=a

View Answer
Answer: b
Explanation: The lines “a = b” and “b = c” should be added to complete the above code.

5. Which of the following recurrence relations can be used to find the nth fibonacci number?
a) F(n) = F(n) + F(n – 1)
b) F(n) = F(n) + F(n + 1)
c) F(n) = F(n – 1)
d) F(n) = F(n – 1) + F(n – 2)
View Answer
Answer: d
Explanation: The relation F(n) = F(n – 1) + F(n – 2) can be used to find the nth fibonacci
number.
6. Consider the following recursive implementation to find the nth fibonacci number:

int fibo(int n)
{
if(n == 1)
return 0;
else if(n == 2)
return 1;
return ________;
}
int main()
{
int n = 5;
int ans = fibo(n);
printf("%d",ans);
return 0;
}
Which of the following lines should be inserted to complete the above code?
a) fibo(n – 1)
b) fibo(n – 1) + fibo(n – 2)
c) fibo(n) + fibo(n – 1)
d) fibo(n – 2) + fibo(n – 1)
View Answer
Answer: b
Explanation: The line fibo(n – 1) + fibo(n – 2) should be inserted to complete the above code.
7. Consider the following recursive implementation to find the nth fibonnaci number:

int fibo(int n)
{
if(n == 1)
return 0;
else if(n == 2)
return 1;
return fibo(n - 1) + fibo(n - 2);
}
int main()
{
int n = 5;
int ans = fibo(n);
printf("%d",ans);
return 0;
}
Which of the following is the base case?
a) if(n == 1)
b) else if(n == 2)
c) return fibo(n – 1) + fibo(n – 2)
d) both if(n == 1) and else if(n == 2)
View Answer
Answer: d
Explanation: Both if(n == 1) and else if(n == 2) are the base cases.
8. What is the time complexity of the following recursive implementation to find the nth
fibonacci number?

int fibo(int n)
{
if(n == 1)
return 0;
else if(n == 2)
return 1;
return fibo(n - 1) + fibo(n - 2);
}
int main()
{
int n = 5;
int ans = fibo(n);
printf("%d",ans);
return 0;
}
a) O(1)
b) O(2*n)
c) O(n2)
d) O(2n)
View Answer
Answer: d
Explanation: The time complexity of the above recursive implementation to find the nth
fibonacci number is O(2n).
9. What is the space complexity of the following recursive implementation to find the nth
fibonacci number?

int fibo(int n)
{
if(n == 1)
return 0;
else if(n == 2)
return 1;
return fibo(n - 1) + fibo(n - 2);
}
int main()
{
int n = 5;
int ans = fibo(n);
printf("%d",ans);
return 0;
}
a) O(1)
b) O(2*n)
c) O(n2)
d) O(2n)
View Answer
Answer: a
Explanation: The space complexity of the above recursive implementation to find the nth
fibonacci number is O(1).
10. What is the output of the following code?

int fibo(int n)
{
if(n == 1)
return 0;
else if(n == 2)
return 1;
return fibo(n - 1) + fibo(n - 2);
}
int main()
{
int n = -1;
int ans = fibo(n);
printf("%d",ans);
return 0;
}
a) 0
b) 1
c) Compile time error
d) Runtime error
View Answer
Answer: d
Explanation: Since negative numbers are not handled by the program, the function fibo() will
be called infinite times and the program will produce a runtime error when the stack
overflows.
11. What is the output of the following code?

int fibo(int n)
{
if(n == 1)
return 0;
else if(n == 2)
return 1;
return fibo(n - 1) + fibo(n - 2);
}
int main()
{
int n = 5;
int ans = fibo(n);
printf("%d",ans);
return 0;
}
a) 1
b) 2
c) 3
d) 5
View Answer
Answer: c
Explanation: The program prints the 5th fibonacci number, which is 3.
12. How many times will the function fibo() be called when the following code is executed?

int fibo(int n)
{
if(n == 1)
return 0;
else if(n == 2)
return 1;
return fibo(n - 1) + fibo(n - 2);
}
int main()
{
int n = 5;
int ans = fibo(n);
printf("%d",ans);
return 0;
}
a) 5
b) 6
c) 8
d) 9
View Answer
Answer: d
Explanation: The function fibo() will be called 9 times, when the above code is executed.
13. What is the output of the following code?

int fibo(int n)
{
if(n == 1)
return 0;
else if(n == 2)
return 1;
return fibo(n - 1) + fibo(n - 2);
}
int main()
{
int n = 10;
int ans = fibo(n);
printf("%d",ans);
return 0;
}
a) 21
b) 34
c) 55
d) 13
View Answer
Answer: b
Explanation: The program prints the 10th fibonacci number, which is 34.

You might also like