Questions
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

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 explanation

Log 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!

More Practical Tools for International Students

Join us and instantly unlock extensive past papers & exclusive solutions to get a head start on your studies!