Questions
Fal25_CS003C_74229 Ch. 5 Pre-Quiz (Remotely Proctored)
Single choice
Which of the following code snippets returns the factorial of a given number? (Hint: Factorial of 5 = 5! = 1 * 2 * 3 * 4 * 5 = 120)
Options
A.def factorial(num) :
if(num == 1) :
return 1
else :
print(num * factorial(num - 1))
B.def factorial(num) :
if(num == 1) :
return 1
else :
return num * factorial(num)
C.def factorial(num) :
return num * factorial(num - 1)
D.def factorial(num) :
if(num == 1) :
return 1
else :
return num * factorial(num - 1)
View Explanation
Verified Answer
Please login to view
Step-by-Step Analysis
To begin, let's evaluate what a correct factorial function must do. It needs a base case that stops recursion (commonly when num reaches 1) and a recursive case that reduces the problem size by calling itself with num - 1, multiplying the result by the current num.
Option 1: The function computes the factorial but uses print instead of return in the recursive branch.......Login to view full explanationLog in for full answers
We've collected over 50,000 authentic exam questions and detailed explanations from around the globe. Log in now and get instant access to the answers!
Similar Questions
Which of the following code snippets returns the factorial of a given number? (Hint: Factorial of 5 = 5! = 1 * 2 * 3 * 4 * 5 = 120)
What is a base case in recursion?
What will this recursive function print when called with countdown(3)?
Which of the following situations might cause a recursive function to run infinitely?
More Practical Tools for International Students
Making Your Study Simpler
Join us and instantly unlock extensive past papers & exclusive solutions to get a head start on your studies!