refactor(curriculum): recursion quiz questions (#58488)

Co-authored-by: Dario-DC <105294544+Dario-DC@users.noreply.github.com>
This commit is contained in:
Jessica Wilkins
2025-02-03 10:24:54 -08:00
committed by GitHub
parent eb4b9715ec
commit e876e1cea0

View File

@@ -7,7 +7,7 @@ dashedName: quiz-recursion
# --description--
To pass the quiz, you must correctly answer at least 18 of the 20 questions below.
To pass the quiz, you must correctly answer at least 9 of the 10 questions below.
# --quizzes--
@@ -43,7 +43,7 @@ Which of the following is an example of recursion?
#### --distractors--
```javascript
```js
function factorial(n) {
let result = 1;
@@ -58,7 +58,7 @@ function factorial(n) {
---
```javascript
```js
function factorial(n) {
const arr = Array(n).fill().map((_, i) => i + 1);
return arr.reduce((acc, curr) => acc * curr, 1);
@@ -67,7 +67,7 @@ function factorial(n) {
---
```javascript
```js
function factorial(n) {
let result = 1;
@@ -81,7 +81,7 @@ function factorial(n) {
#### --answer--
```javascript
```js
function factorial(n) {
if (n === 0 || n === 1) {
return 1;
@@ -94,9 +94,9 @@ function factorial(n) {
#### --text--
What does the following function return?
What will the following function return?
```javascript
```js
function sum(n) {
if (n === 0) return 0;
return n + sum(n - 1);
@@ -114,7 +114,7 @@ sum(3);
---
A `Too much recursion` error.
1
#### --answer--
@@ -124,87 +124,7 @@ A `Too much recursion` error.
#### --text--
Given the following recursion:
```javascript
function sum(n) {
if (n === 0) return 0;
return n + sum(n - 1);
}
sum(3);
```
Which of the following is the order of the function calls in the call stack?
#### --distractors--
```js
sum(0);
sum(1);
sum(2);
sum(3);
```
---
```js
sum(1);
sum(2);
sum(3);
```
---
```js
sum(3);
sum(2);
sum(1);
```
#### --answer--
```js
sum(3);
sum(2);
sum(1);
sum(0);
```
### --question--
#### --text--
What will this code print?
```javascript
function sayHello(n) {
if (n <= 0) return [];
return [`Hello ${n}`, ...sayHello(n - 1)];
}
console.log(sayHello(3));
```
#### --distractors--
"Hello 3"
---
An error.
---
`["Hello 3", "...Hello"]`
#### --answer--
`["Hello 3", "Hello 2", "Hello 1"]`
### --question--
#### --text--
How many times will the function call itself?
How many times will the `mystery` function be called?
```javascript
function mystery(n) {
@@ -234,110 +154,29 @@ mystery(5);
#### --text--
When should you use recursion?
Which of the following is true about recursion?
#### --distractors--
All the time. Recursion is more superior than a traditional loop.
It should never have a base case.
---
In problems that require working with numbers.
It should only be used for the fibonacci sequence.
---
In problems that require working with strings.
It should only be used for factorials.
#### --answer--
In problems that involve performing repetitive tasks, or working with deeply nested arrays or objects, or tree-like structures.
It should always have a base case.
### --question--
#### --text--
Which of the following is NOT true about recursion?
#### --distractors--
It must have a base case.
---
It can replace loops.
---
It can be memory-intensive.
#### --answer--
It always returns 0.
### --question--
#### --text--
The following code handles a click event using recursion:
```javascript
element.onclick = function() {
this.onclick();
}
```
What is the risk of using this approach?
#### --distractors--
Infinite event loop.
---
Browser crash.
---
Memory leak.
#### --answer--
All of the above.
### --question--
#### --text--
What's wrong with this recursive DOM traversal?
```javascript
function findElement(element) {
if (element.id === 'target') return element;
findElement(element.firstChild);
}
```
#### --distractors--
Missing base case.
---
Wrong parameter.
---
Incorrect comparison.
#### --answer--
Missing return for recursive call.
### --question--
#### --text--
The following recursion is missing a base case:
Which of the following options would be an appropriate base case for the given example?
```js
function countDownToZero(number) {
@@ -348,8 +187,6 @@ function countDownToZero(number) {
}
```
Which of the following options should be its base case?
#### --distractors--
```js
@@ -398,7 +235,7 @@ To reduce the number of function calls.
---
To ensure all recursive calls are executed in the correct order.
To ensure all recursive calls are executed in the correct order and a `RecursionError` is not thrown.
#### --answer--
@@ -408,33 +245,11 @@ To provide a way for the function to break out of its recursive calls and preven
#### --text--
Which of the following is a potential drawback of using recursion?
#### --distractors--
The webpage takes longer to load.
---
Recursion makes it impossible to debug the application.
---
Recursion only works with large datasets.
#### --answer--
Recursion is complicated and can make the code harder to understand.
### --question--
#### --text--
What will this recursive function do?
```javascript
function repeat(str) {
return str + repeat(str);
```js
function repeatString(str) {
return str + repeatString(str);
}
```
@@ -458,28 +273,6 @@ Cause a stack overflow.
#### --text--
What HTML structure is typically traversed recursively?
#### --distractors--
Style sheets.
---
Meta tags.
---
Script tags.
#### --answer--
The DOM tree.
### --question--
#### --text--
What is a call stack?
#### --distractors--
@@ -492,7 +285,7 @@ A list of values that were returned by a function.
---
A list of function declarations.
A special function used to call a function exactly three times.
#### --answer--
@@ -502,31 +295,9 @@ A data structure that keeps track of function calls and their execution order.
#### --text--
What happens when a call stack exceeds its maximum size?
#### --distractors--
The webpage is automatically reloaded.
---
All functions will be executed at once.
---
The browser will immediately empty the stack.
#### --answer--
A stack overflow error is thrown.
### --question--
#### --text--
What does this recursive function do?
```javascript
```js
function changeString(str) {
if (str === "") return "";
return changeString(str.slice(1)) + str[0];
@@ -549,83 +320,3 @@ Removes spaces.
Reverses a string.
### --question--
#### --text--
Which of the following is the correct execution order of a call stack?
#### --distractors--
All function calls are executed simultaneously.
---
All function calls are executed in a random order.
---
First in, first out.
#### --answer--
Last in, first out.
### --question--
#### --text--
Given the following code:
```javascript
function foo() {
console.log("foo");
baz();
}
function bar() {
console.log("bar");
foo();
}
function baz() {
console.log("baz");
}
bar();
```
What will be printed in the console?
#### --distractors--
```javascript
"bar"
"baz"
"foo"
```
---
```javascript
"foo"
"baz"
"bar"
```
---
```javascript
"baz"
"foo"
"bar"
```
#### --answer--
```javascript
"bar"
"foo"
"baz"
```