Data Structure Multiple Choice Questions on “Factorial using Recursion”.
1. In general, which of the following methods isn’t used to find the factorial of a number? Answer: d 2. Which of the following recursive formula can be used to find the factorial of a number? Answer: c 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? a) fact = fact + i 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? a) n = 0 Answer: c 5. The time complexity of the following recursive implementation to find the factorial of a number is ________ a) O(1) Answer: b 6. What is the space complexity of the following recursive implementation to find the factorial of a number? a) O(1) Answer: a 7. Consider the following recursive implementation to find the factorial of a number. Which of the lines is the base case? a) return 1 Answer: c 8. What is the output of the following code? a) 0 Answer: b 9. What is the output of the following code? a) 0 Answer: b 10. How many times will the function fact() be called when the following code is executed? a) 4 Answer: c 11. What is the output of the following code? a) 24 Answer: b complete set of 1000+
a) Recursion
b) Iteration
c) Dynamic programming
d) Non iterative / recursive
Clarification: 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.
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)
Clarification: fact(n) = n * fact(n – 1) can be used 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;
}
b) fact = fact * i
c) i = i * fact
d) i = i + fact
Answer: b
Clarification: The line “fact = fact * i” should be inserted to complete the above 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;
}
b) n != 0
c) n == 0
d) n == 1
Clarification: 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.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;
}
b) O(n)
c) O(n2)
d) O(n3)
Clarification: The time complexity of the above recursive implementation to find the factorial of a number is O(n).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;
}
b) O(n)
c) O(n2)
d) O(n3)
View Answer
Clarification: The space complexity of the above recursive implementation to find the factorial of a number is O(1).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;
}
b) return n * fact(n-1)
c) if(n == 0)
d) if(n == 1)
View Answer
Clarification: The line “if(n == 0)” is the base case.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;
}
b) 1
c) 2
d) 3
View Answer
Clarification: The program prints 0!, which is 1.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;
}
b) 1
c) 2
d) 3
View Answer
Clarification: The program prints 1!, which is 1.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;
}
b) 5
c) 6
d) 7
View Answer
Clarification: The fact() function will be called 6 times with the following arguments:
fact(5), fact(4), fact(3), fact(2), fact(1), fact(0).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;
}
b) 120
c) 720
d) 1
Clarification: The function prints 5!, which is 120.