This set of Data Structure Multiple Choice Questions & Answers (MCQs) focuses on “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
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
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
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
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
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
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
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
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
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
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
Explanation: The function prints 5!, which is 120.
Sanfoundry Global Education & Learning Series – Data Structures & Algorithms.
To practice all areas of Data Structures & Algorithms, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Practice Data Structure MCQ
- Check Design and Analysis of Algorithms Books
- Practice Programming MCQs
- Check Computer Science Books
- Check Programming Books