Week 14 Quiz
Predict, then check: what does this program print?
def countdown(n): if n == 0: print("Go!") return print(n) countdown(n - 1) countdown(3)Select one.
- Go!, then 3, 2, 1, one per line
- 3, 2, 1, then Go!, one per line
- 3, 2, 1, one per line, and nothing else
- 3, 2, 1, 0, then Go!, one per line
Predict, then check: what does this program print?
def factorial(n): if n == 0: return 1 return n * factorial(n - 1) print(factorial(5))Select one.
- 120
- 24
- 0
- RecursionError
The pinned factorial below prints a line each time a call starts. How many lines that begin with call does factorial(5) print?
def factorial(n, depth=0): print(" " * depth + "call factorial(" + str(n) + ")") if n == 0: print(" " * depth + "return 1") return 1 result = n * factorial(n - 1, depth + 1) print(" " * depth + "return " + str(result)) return result print(factorial(5))Select one.
- 4
- 5
- 6
- 120
Predict, then check: what does this program print?
def countup(n): if n == 0: print("Go!") return countup(n - 1) print(n) countup(3)Select one.
- 3, 2, 1, then Go!, one per line
- Go!, then 3, 2, 1, one per line
- 1, 2, 3, then Go!, one per line
- Go!, then 1, 2, 3, one per line
Predict, then check: what is the last line this program prints?
def countdown(n): if n == 0: print("Go!") return print(n) countdown(n - 1) result = countdown(3) print(result)Select one.
- Go!
- None
- 3
- 0
Predict, then check: what does this program print?
def bad(n): return n + bad(n - 1) print(bad(5))Select one.
- 15
- 0
- RecursionError
- IndexError
Predict, then check: what does this program print?
def total_pages(daily): if daily == []: return 0 return daily[0] + total_pages(daily[1:]) print(total_pages([12, 40, 25, 0, 33, 17, 27]))Select one.
- 154
- 142
- 22.0
- 0
Select every line this program prints.
def sum_to(n): if n == 0: return 0 return n + sum_to(n - 1) def sum_to_iter(n): total = 0 for i in range(1, n + 1): total = total + i return total print(sum_to(5)) print(sum_to_iter(5)) print(sum_to(100))Select every option that applies.
- 15
- 5050
- 10
- 14
- 4950
In the call countdown(3), what is the 3?
Select one.
- the parameter
- the base case
- the return value
- the argument
True or false: a recursive sum_to whose recursive call passes the same n, as in return n + sum_to(n), still reaches its base case for sum_to(5) as long as the test if n == 0: is present.
True or false.
- True
- False
Built with The Course Maker · samples.thecoursemaker.com