All quizzesMedium
Functions & Scope — Series 2
Preview — 3 of 10 questions
What is the output of the following code?
javascript
def factorial(n):
if n == 0:
return 1
return n * factorial(n - 1)
print(factorial(5))
print(factorial(0))
def count_down(n):
if n < 0:
return []
return [n] + count_down(n - 1)
print(count_down(3))A120, 1, [3, 2, 1, 0, -1] — the recursion never actually reaches a base case for count_down.
B120, 0, [3, 2, 1, 0]
C120, 1, [3, 2, 1, 0]
DRecursionError: maximum recursion depth exceeded on factorial(0)
What is the output of the following code?
javascript
people = [("Bob", 30), ("Ada", 30), ("Cid", 25)]
result = sorted(people, key=lambda p: (p[1], p[0]))
print(result)A[('Cid', 25), ('Bob', 30), ('Ada', 30)]
B[('Bob', 30), ('Ada', 30), ('Cid', 25)]
C[('Ada', 30), ('Bob', 30), ('Cid', 25)]
D[('Cid', 25), ('Ada', 30), ('Bob', 30)]
What is the output of the following code?
javascript
def parse(value):
try:
return int(value)
except ValueError as e:
raise RuntimeError("parsing failed") from e
try:
parse("abc")
except RuntimeError as err:
print(err)
print(type(err.__cause__))Aparsing failed, then <class 'ValueError'>
Bparsing failed, then None
Cinvalid literal for int() with base 10: 'abc', then <class 'ValueError'>
DOnly the original ValueError propagates — the RuntimeError never triggers.
Sign up free to play
Answer all 10 questions (7 more), see explanations for every answer, and track your score.