Self-assessment quiz

Presentation & objectives

The following quizzes are here to help you check that you understood the articles you had to study. At the end of a quiz, you will be given explanations on your answers. If some of them are wrong, you will have the possibility to click on the question you failed to try again.

These quizzes are provided for self-assessment and will not be graded or stored.

Don’t hesitate to reach out on the Discord server for any precision/explanation!

Quizzes

Recursive algorithms

# What sentence best describes a base case in a recursive function? 1. [x] The part of the function where the recursion stops and a result is returned > ✅ The base case is essential to ensure recursion terminates, providing a result instead of calling the function again. 1. [ ] The part of the function that calls itself > ❌ This describes recursion itself, not the base case which is crucial to stop recursion. 1. [ ] The maximum depth the recursion can reach > ❌ Recursion depth is a different concept, usually relating to system limitations, not the base case. 1. [ ] The initial call to the recursive function > ❌ The initial call is simply the first invocation of the recursive function, not the base case. # Given the following recursive function, what will be the output of `foo(3)`? ```python def foo (n: int) -> int: if n == 0: return 1 else: return n - 1 * foo(n) ``` 1. [ ] 6 > ❌ This is not the correct output due to issues in the recursive logic. 1. [x] The input is not getting "smaller", this leads to infinite recursion > ✅ The input does not decrease, causing the function to keep calling itself indefinitely, leading to infinite recursion. 1. [ ] The base case is incorrect, this leads to infinite recursion > ❌ The base case is valid (`n == 0`), but the problem is with the recursive step, not reducing `n` properly. 1. [ ] The recursive call is not always connected to the base: this may lead to infinite recursion > ❌ The recursive call is connected to the base, but the problem lies in the way `n` is handled. # Given the following recursive function, what will be the output of `bar(4)`? ```python def bar (n: int) -> int: if n == 0: return 1 else: return n + bar(n - 2) ``` 1. [x] 7 > ✅ The recursive calls would be: `4 + bar(2)` which returns `4 + (2 + bar(0)) = 4 + 2 + 1 = 7`. 1. [ ] The input is not getting "smaller", this leads to infinite recursion > ❌ The input does decrease by 2 each time, preventing infinite recursion. 1. [ ] The base case is incorrect, this leads to infinite recursion > ❌ The base case is correct (`n == 0`), allowing the recursion to stop. 1. [ ] The recursive call is not always connected to the base: this may lead to infinite recursion > ❌ The recursive call is correctly reducing the input and is connected to the base case. # Given the following recursive function, what will be the output of `bar(3)`? ```python def bar (n: int) -> int: if n == 0: return 1 else: return n + bar(n - 2) ``` 1. [ ] 4 > ❌ This is not the correct output based on how the recursive function progresses. 1. [ ] The input is not getting "smaller", this leads to infinite recursion > ❌ The input does decrease by 2, so it is shrinking. 1. [ ] The base case is incorrect, this leads to infinite recursion > ❌ The base case is correct and would stop the recursion. 1. [x] The recursive call is not always connected to the base: this may lead to infinite recursion > ✅ Starting with `bar(3)`, it calls `bar(1)`, and then `bar(-1)` and so on, but there's no base case to handle `n < 0`, leading to potential infinite recursion.