Hướng dẫn thử thách
1 / 1
Recursion Quiz
To pass the quiz, you must correctly answer at least 9 of the 10 questions below.
# --quizzes--
## --quiz--
### --question--
#### --text--
What is recursion in programming?
#### --distractors--
A method of sorting lists.
---
A loop that never ends.
---
A function that returns `None`.
#### --answer--
A process in which a function calls itself.
### --question--
#### --text--
Which of the following is an example of recursion?
#### --distractors--
```py
def factorial(n):
result = 1
while n > 0:
result *= n
n -= 1
return result
```
---
```py
def factorial(n):
numbers = [number for number in range(1, n + 1)]
result = 1
for number in numbers:
result *= number
return result
```
---
```py
def factorial(n):
result = 1
for number in range(n, 0, -1):
result *= number
return result
```
#### --answer--
```py
def factorial(n):
if n == 0 or n == 1:
return 1
return n * factorial(n - 1)
```
### --question--
#### --text--
What will the following function return?
```py
def sum_numbers(n):
if n == 0:
return 0
return n + sum_numbers(n - 1)
sum_numbers(3)
```
#### --distractors--
3
---
0
---
1
#### --answer--
6
### --question--
#### --text--
How many times will the `mystery` function be called?
```py
def mystery(n):
if n <= 1:
return 1
return mystery(n - 2)
mystery(5)
```
#### --distractors--
2
---
5
---
4
#### --answer--
3
### --question--
#### --text--
Which of the following is true about recursion?
#### --distractors--
It should never have a base case.
---
It should only be used for the Fibonacci sequence.
---
It should only be used for factorials.
#### --answer--
It should always have a base case.
### --question--
#### --text--
Which of the following options would be an appropriate base case for the given example?
```py
def count_down_to_zero(number):
# Base case goes here.
print(number)
count_down_to_zero(number - 1)
```
#### --distractors--
```py
if number > 0:
return
```
---
```py
if number != 0:
return
```
---
```py
if number == 0:
return
```
#### --answer--
```py
if number < 0:
return
```
### --question--
#### --text--
Why must recursion have a base case?
#### --distractors--
To ensure the function always has a return value.
---
To reduce the number of function calls.
---
To ensure all recursive calls are executed in the correct order.
#### --answer--
To provide a way for the function to stop making recursive calls and avoid a `RecursionError`.
### --question--
#### --text--
What will this recursive function do?
```py
def repeat_string(string):
return string + repeat_string(string)
```
#### --distractors--
Return the string twice.
---
Return `None`.
---
Create an empty string.
#### --answer--
Raise a `RecursionError`.
### --question--
#### --text--
What is a call stack?
#### --distractors--
A list of function calls that have been executed.
---
A list of values that were returned by a function.
---
A special function used to call a function exactly three times.
#### --answer--
A data structure that keeps track of function calls and their execution order.
### --question--
#### --text--
What does this recursive function do?
```py
def change_string(string):
if string == "":
return ""
return change_string(string[1:]) + string[0]
```
#### --distractors--
Duplicates a string.
---
Removes vowels.
---
Removes spaces.
#### --answer--
Reverses a string.
Vượt qua bài kiểm tra hiện tại để mở khóa bài tiếp theo.
main.py
UTF-8 • Tab Size: 2Kiểm tra bài:⌘↵