feat(curriculum): new questions for front end recursion quiz. (#57016)

Co-authored-by: Huyen Nguyen <25715018+huyenltnguyen@users.noreply.github.com>
This commit is contained in:
Peter Cruckshank
2024-11-27 17:04:45 -05:00
committed by GitHub
parent 7d4f01263f
commit e29320f947
@@ -17,439 +17,615 @@ To pass the quiz, you must correctly answer at least 17 of the 20 questions belo
#### --text--
Placeholder question
What is recursion in programming?
#### --distractors--
Placeholder distractor 1
A method of sorting arrays.
---
Placeholder distractor 2
A loop that never ends.
---
Placeholder distractor 3
A function that returns `undefined`.
#### --answer--
Placeholder answer
A process in which a function calls itself.
### --question--
#### --text--
Placeholder question
Which of the following is an example of recursion?
#### --distractors--
Placeholder distractor 1
```javascript
function factorial(n) {
let result = 1;
while (n > 0) {
result = result * n;
n--;
}
return result;
}
```
---
Placeholder distractor 2
```javascript
function factorial(n) {
const arr = Array(n).fill().map((_, i) => i + 1);
return arr.reduce((acc, curr) => acc * curr, 1);
}
```
---
Placeholder distractor 3
```javascript
function factorial(n) {
let result = 1;
for (let i = n; i > 0; i--) {
result *= i;
}
return result;
}
```
#### --answer--
Placeholder answer
```javascript
function factorial(n) {
if (n === 0 || n === 1) {
return 1;
}
return n * factorial(n - 1);
}
```
### --question--
#### --text--
Placeholder question
What does the following function return?
```javascript
function sum(n) {
if (n === 0) return 0;
return n + sum(n - 1);
}
sum(3);
```
#### --distractors--
Placeholder distractor 1
3
---
Placeholder distractor 2
0
---
Placeholder distractor 3
A `Too much recursion` error.
#### --answer--
Placeholder answer
6
### --question--
#### --text--
Placeholder question
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--
Placeholder distractor 1
```js
sum(0);
sum(1);
sum(2);
sum(3);
```
---
Placeholder distractor 2
```js
sum(1);
sum(2);
sum(3);
```
---
Placeholder distractor 3
```js
sum(3);
sum(2);
sum(1);
```
#### --answer--
Placeholder answer
```js
sum(3);
sum(2);
sum(1);
sum(0);
```
### --question--
#### --text--
Placeholder question
What will this code print?
```javascript
function sayHello(n) {
if (n <= 0) return [];
return [`Hello ${n}`, ...sayHello(n - 1)];
}
console.log(sayHello(3));
```
#### --distractors--
Placeholder distractor 1
"Hello 3"
---
Placeholder distractor 2
An error
---
Placeholder distractor 3
`["Hello 3", "...Hello"]`
#### --answer--
Placeholder answer
`["Hello 3", "Hello 2", "Hello 1"]`
### --question--
#### --text--
Placeholder question
How many times will the function call itself?
```javascript
function mystery(n) {
if (n <= 1) return 1;
return mystery(n - 2);
}
mystery(5);
```
#### --distractors--
Placeholder distractor 1
2
---
Placeholder distractor 2
5
---
Placeholder distractor 3
4
#### --answer--
Placeholder answer
3
### --question--
#### --text--
Placeholder question
When should you use recursion?
#### --distractors--
Placeholder distractor 1
All the time. Recursion is more superior than a traditional loop.
---
Placeholder distractor 2
In problems that require working with numbers.
---
Placeholder distractor 3
In problems that require working with strings.
#### --answer--
Placeholder answer
In problems that involve performing repetitive tasks, or working with deeply nested arrays or objects, or tree-like structures.
### --question--
#### --text--
Placeholder question
Which of the following is NOT true about recursion?
#### --distractors--
Placeholder distractor 1
It must have a base case.
---
Placeholder distractor 2
It can replace loops.
---
Placeholder distractor 3
It can be memory-intensive.
#### --answer--
Placeholder answer
It always returns 0.
### --question--
#### --text--
Placeholder question
The following code handles a click event using recursion:
```javascript
element.onclick = function() {
this.onclick();
}
```
What is the risk of using this approach?
#### --distractors--
Placeholder distractor 1
Infinite event loop.
---
Placeholder distractor 2
Browser crash.
---
Placeholder distractor 3
Memory leak.
#### --answer--
Placeholder answer
All of the above.
### --question--
#### --text--
Placeholder question
What's wrong with this recursive DOM traversal?
```javascript
function findElement(element) {
if (element.id === 'target') return element;
findElement(element.firstChild);
}
```
#### --distractors--
Placeholder distractor 1
Missing base case.
---
Placeholder distractor 2
Wrong parameter.
---
Placeholder distractor 3
Incorrect comparison.
#### --answer--
Placeholder answer
Missing return for recursive call.
### --question--
#### --text--
Placeholder question
The following recursion is missing a base case:
```js
function countDownToZero(number) {
// Base case goes here
console.log(number);
countDownToZero(number - 1);
}
```
Which of the following options should be its base case?
#### --distractors--
Placeholder distractor 1
```js
if (number > 0) {
return;
}
```
---
Placeholder distractor 2
```js
if (number !== 0) {
return;
}
```
---
Placeholder distractor 3
```js
if (number === 0) {
return;
}
```
#### --answer--
Placeholder answer
```js
if (number < 0) {
return;
}
```
### --question--
#### --text--
Placeholder question
Why must recursion have a base case?
#### --distractors--
Placeholder distractor 1
To ensure the function always has a return value.
---
Placeholder distractor 2
To reduce the number of function calls.
---
Placeholder distractor 3
To ensure all recursive calls are executed in the correct order.
#### --answer--
Placeholder answer
To provide a way for the function to break out of its recursive calls and prevent infinite loops.
### --question--
#### --text--
Placeholder question
Which of the following is a potential drawback of using recursion?
#### --distractors--
Placeholder distractor 1
The webpage takes longer to load.
---
Placeholder distractor 2
Recursion makes it impossible to debug the application.
---
Placeholder distractor 3
Recursion only works with large datasets.
#### --answer--
Placeholder answer
Recursion is complicated and can make the code harder to understand.
### --question--
#### --text--
Placeholder question
What will this recursive function do?
```javascript
function repeat(str) {
return str + repeat(str);
}
```
#### --distractors--
Placeholder distractor 1
Return the string twice.
---
Placeholder distractor 2
Return `undefined`.
---
Placeholder distractor 3
Create an empty string.
#### --answer--
Placeholder answer
Cause a stack overflow.
### --question--
#### --text--
Placeholder question
What HTML structure is typically traversed recursively?
#### --distractors--
Placeholder distractor 1
Style sheets
---
Placeholder distractor 2
Meta tags
---
Placeholder distractor 3
Script tags
#### --answer--
Placeholder answer
The DOM tree
### --question--
#### --text--
Placeholder question
What is a call stack?
#### --distractors--
Placeholder distractor 1
A list of function calls that have been executed.
---
Placeholder distractor 2
A list of values that were returned by a function.
---
Placeholder distractor 3
A list of function declarations.
#### --answer--
Placeholder answer
A data structure that keeps track of function calls and their execution order.
### --question--
#### --text--
Placeholder question
What happens when a call stack exceeds its maximum size?
#### --distractors--
Placeholder distractor 1
The webpage is automatically reloaded.
---
Placeholder distractor 2
All functions will be executed at once.
---
Placeholder distractor 3
The browser will immediately empty the stack.
#### --answer--
Placeholder answer
A stack overflow error is thrown.
### --question--
#### --text--
Placeholder question
What does this recursive function do?
```javascript
function changeString(str) {
if (str === "") return "";
return changeString(str.slice(1)) + str[0];
}
```
#### --distractors--
Placeholder distractor 1
Duplicates a string.
---
Placeholder distractor 2
Removes vowels.
---
Placeholder distractor 3
Removes spaces.
#### --answer--
Placeholder answer
Reverses a string.
### --question--
#### --text--
Placeholder question
Which of the following is the correct execution order of a call stack?
#### --distractors--
Placeholder distractor 1
All function calls are executed simultaneously.
---
Placeholder distractor 2
All function calls are executed in a random order.
---
Placeholder distractor 3
First in, first out.
#### --answer--
Placeholder answer
Last in, first out.
### --question--
#### --text--
Placeholder question
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--
Placeholder distractor 1
```javascript
"bar"
"baz"
"foo"
```
---
Placeholder distractor 2
```javascript
"foo"
"baz"
"bar"
```
---
Placeholder distractor 3
```javascript
"baz"
"foo"
"bar"
```
#### --answer--
Placeholder answer
```javascript
"bar"
"foo"
"baz"
```