Data Structure Questions and Answers on Factorial using Recursion for Freshers

https://www.computersprofessor.com/2017/11/data-structure-questions-and-answers-on_14.html
1. Which of the following methods can be used to find the factorial of a number?
a) Recursion
b) Iteration
c) Dynamic programming
d) All of the mentioned
Answer: d
Explanation: All of the above mentioned methods can be used to find the factorial of a number.
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)
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:
int main() { int n = 6, i; int fact = 1; for(i=1;i<=n;i++) _________; printf("%d",fact); return 0; }
Which of the following lines should be inserted to complete the above code?
a) fact = fact + i
b) fact = fact * i
c) i = i * fact
d) i = i + fact
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:
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; }
Which of the following lines should be inserted to complete the above code?
a) n = 0
b) n != 0
c) n == 0
d) n == 1
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. What is the time complexity of the above recursive implementation to find the factorial of a number?
a) O(1)
b) O(n)
c) O(n2)
d) O(n3)
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 above recursive implementation to find the factorial of a number?
a) O(1)
b) O(n)
c) O(n2)
d) O(n3)
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:
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; }
Which of the following lines is the base case?
a) return 1
b) return n * fact(n-1)
c) if(n == 0)
d) none of the mentioned
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
b) 1
c) 2
d) 3
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
b) 1
c) 2
d) 3
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
b) 5
c) 6
d) 7
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
b) 120
c) 720
d) 1
Answer: b
Explanation: The function prints 5!, which is 120.