mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-28 18:26:54 +00:00
feat(curriculum): Add JavaScript Functions Quiz (#56450)
This commit is contained in:
+143
-100
@@ -17,439 +17,482 @@ Answer all of the questions below correctly to pass the quiz.
|
||||
|
||||
#### --text--
|
||||
|
||||
Placeholder question
|
||||
What is the correct syntax for a function declaration?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
Placeholder distractor 1
|
||||
`var myFunction = function() {};`
|
||||
|
||||
---
|
||||
|
||||
Placeholder distractor 2
|
||||
`myFunction: function() {}`
|
||||
|
||||
---
|
||||
|
||||
Placeholder distractor 3
|
||||
`function = myFunction();`
|
||||
|
||||
#### --answer--
|
||||
|
||||
Placeholder answer
|
||||
`function myFunction() {}`
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
Placeholder question
|
||||
Which of the following is a valid function expression?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
Placeholder distractor 1
|
||||
```js
|
||||
function getSum(x, y) {
|
||||
return x + y;
|
||||
};
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
Placeholder distractor 2
|
||||
```js
|
||||
getSum: function(x, y) {
|
||||
return x + y;
|
||||
};
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
Placeholder distractor 3
|
||||
```js
|
||||
function = getSum(x, y) {
|
||||
return x + y;
|
||||
};
|
||||
```
|
||||
|
||||
#### --answer--
|
||||
|
||||
Placeholder answer
|
||||
```js
|
||||
getSum = function(x, y) {
|
||||
return x + y;
|
||||
};
|
||||
```
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
Placeholder question
|
||||
Where can function expressions be used?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
Placeholder distractor 1
|
||||
Only within other functions
|
||||
|
||||
---
|
||||
|
||||
Placeholder distractor 2
|
||||
Only as part of an object literal
|
||||
|
||||
---
|
||||
|
||||
Placeholder distractor 3
|
||||
Only in the global scope
|
||||
|
||||
#### --answer--
|
||||
|
||||
Placeholder answer
|
||||
Anywhere a variable can be used
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
Placeholder question
|
||||
What is the difference between a function expression and a function declaration?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
Placeholder distractor 1
|
||||
Function expressions can be hoisted, while function declarations cannot.
|
||||
|
||||
---
|
||||
|
||||
Placeholder distractor 2
|
||||
Both function expressions and function declarations can be hoisted.
|
||||
|
||||
---
|
||||
|
||||
Placeholder distractor 3
|
||||
Neither function expressions nor function declarations can be hoisted.
|
||||
|
||||
#### --answer--
|
||||
|
||||
Placeholder answer
|
||||
Function declarations can be hoisted, while function expressions cannot.
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
Placeholder question
|
||||
What does it mean for functions to be first-class citizens in JavaScript?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
Placeholder distractor 1
|
||||
They can be assigned to variables.
|
||||
|
||||
---
|
||||
|
||||
Placeholder distractor 2
|
||||
They can be passed as arguments to other functions.
|
||||
|
||||
---
|
||||
|
||||
Placeholder distractor 3
|
||||
They can be returned from functions.
|
||||
|
||||
#### --answer--
|
||||
|
||||
Placeholder answer
|
||||
All of the answers are correct.
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
Placeholder question
|
||||
Which of the following is a common use case for first-class functions in JavaScript?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
Placeholder distractor 1
|
||||
Implementing object-oriented programming
|
||||
|
||||
---
|
||||
|
||||
Placeholder distractor 2
|
||||
Defining global variables
|
||||
|
||||
---
|
||||
|
||||
Placeholder distractor 3
|
||||
Creating custom data types
|
||||
|
||||
#### --answer--
|
||||
|
||||
Placeholder answer
|
||||
Creating higher-order functions
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
Placeholder question
|
||||
Which of the following is an example of a first-class function?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
Placeholder distractor 1
|
||||
```js
|
||||
function greet(name) {
|
||||
console.log("Hello, " + name);
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
Placeholder distractor 2
|
||||
```js
|
||||
var greet = function(name) {
|
||||
console.log("Hello, " + name);
|
||||
};
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
Placeholder distractor 3
|
||||
```js
|
||||
const greet = (name) => {
|
||||
console.log("Hello, " + name);
|
||||
}
|
||||
```
|
||||
|
||||
#### --answer--
|
||||
|
||||
Placeholder answer
|
||||
All of the answers are correct.
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
Placeholder question
|
||||
Which of the following is not a common use case for first-class functions?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
Placeholder distractor 1
|
||||
Defining object methods
|
||||
|
||||
---
|
||||
|
||||
Placeholder distractor 2
|
||||
Implementing closures
|
||||
|
||||
---
|
||||
|
||||
Placeholder distractor 3
|
||||
Creating callbacks
|
||||
|
||||
#### --answer--
|
||||
|
||||
Placeholder answer
|
||||
Creating custom data types
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
Placeholder question
|
||||
What is the syntax for an arrow function?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
Placeholder distractor 1
|
||||
`myFunction: function() {}`
|
||||
|
||||
---
|
||||
|
||||
Placeholder distractor 2
|
||||
`function myFunction() {}`
|
||||
|
||||
---
|
||||
|
||||
Placeholder distractor 3
|
||||
`const myFunction = function() {};`
|
||||
|
||||
#### --answer--
|
||||
|
||||
Placeholder answer
|
||||
`myFunction = () => { ... };`
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
Placeholder question
|
||||
What is the difference between a regular function and an arrow function?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
Placeholder distractor 1
|
||||
Arrow functions do not have their own 'this' binding.
|
||||
|
||||
---
|
||||
|
||||
Placeholder distractor 2
|
||||
Arrow functions cannot be used as constructors.
|
||||
|
||||
---
|
||||
|
||||
Placeholder distractor 3
|
||||
Arrow functions cannot be used with the 'new' keyword.
|
||||
|
||||
#### --answer--
|
||||
|
||||
Placeholder answer
|
||||
All of the answers are correct.
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
Placeholder question
|
||||
You can also call arrow function as
|
||||
|
||||
#### --distractors--
|
||||
|
||||
Placeholder distractor 1
|
||||
Lambda function
|
||||
|
||||
---
|
||||
|
||||
Placeholder distractor 2
|
||||
Fat arrow function
|
||||
|
||||
---
|
||||
|
||||
Placeholder distractor 3
|
||||
Neither Lambda function or Fat arrow function
|
||||
|
||||
#### --answer--
|
||||
|
||||
Placeholder answer
|
||||
Both Lambda function and Fat arrow function
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
Placeholder question
|
||||
What is the benefit of using arrow functions?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
Placeholder distractor 1
|
||||
You can omit curly braces and return keyword if your output is going to be a single line.
|
||||
|
||||
---
|
||||
|
||||
Placeholder distractor 2
|
||||
They provide easy syntax while writing promises and callbacks.
|
||||
|
||||
---
|
||||
|
||||
Placeholder distractor 3
|
||||
They have short syntax and increase readability.
|
||||
|
||||
#### --answer--
|
||||
|
||||
Placeholder answer
|
||||
All of the answers are correct.
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
Placeholder question
|
||||
What is the difference between a function and a method?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
Placeholder distractor 1
|
||||
There is no difference between functions and methods.
|
||||
|
||||
---
|
||||
|
||||
Placeholder distractor 2
|
||||
Functions and methods are properties of objects
|
||||
|
||||
---
|
||||
|
||||
Placeholder distractor 3
|
||||
Functions are properties of objects, while methods are standalone functions.
|
||||
|
||||
#### --answer--
|
||||
|
||||
Placeholder answer
|
||||
Methods are properties of objects, while functions are standalone functions.
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
Placeholder question
|
||||
Can a method be used outside of its object?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
Placeholder distractor 1
|
||||
Yes, methods can be used anywhere a function can be used.
|
||||
|
||||
---
|
||||
|
||||
Placeholder distractor 2
|
||||
Methods can be used outside of their object, but only if they are bound to a specific 'this' value.
|
||||
|
||||
---
|
||||
|
||||
Placeholder distractor 3
|
||||
Methods can be used outside of their object, but only if they are defined as arrow functions.
|
||||
|
||||
#### --answer--
|
||||
|
||||
Placeholder answer
|
||||
No, methods can only be used within their object.
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
Placeholder question
|
||||
Which of the following is an example of a method?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
Placeholder distractor 1
|
||||
```js
|
||||
function greet(name) {
|
||||
console.log("Hello, " + name);
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
Placeholder distractor 2
|
||||
```js
|
||||
var greet = function(name) {
|
||||
console.log("Hello, " + name);
|
||||
};
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
Placeholder distractor 3
|
||||
All of them.
|
||||
|
||||
#### --answer--
|
||||
|
||||
Placeholder answer
|
||||
```js
|
||||
const person = {
|
||||
name: "Alice",
|
||||
greet: function() {
|
||||
console.log("Hello, my name is " + this.name);
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
Placeholder question
|
||||
What is the benefit of using methods instead of functions?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
Placeholder distractor 1
|
||||
Methods provide a more organized way to group related functionality.
|
||||
|
||||
---
|
||||
|
||||
Placeholder distractor 2
|
||||
Methods can be accessed using dot notation, which can make code more readable.
|
||||
|
||||
---
|
||||
|
||||
Placeholder distractor 3
|
||||
Methods have access to the properties and methods of their object.
|
||||
|
||||
#### --answer--
|
||||
|
||||
Placeholder answer
|
||||
All of the answers are correct.
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
Placeholder question
|
||||
What is the difference between arguments and parameters?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
Placeholder distractor 1
|
||||
There is no difference between arguments and parameters.
|
||||
|
||||
---
|
||||
|
||||
Placeholder distractor 2
|
||||
Parameters are the actual values passed to a function when it is called, while arguments are the names of variables used in a function definition.
|
||||
|
||||
---
|
||||
|
||||
Placeholder distractor 3
|
||||
Arguments and parameters are used interchangeably.
|
||||
|
||||
#### --answer--
|
||||
|
||||
Placeholder answer
|
||||
Parameters are the names of variables used in a function definition, while arguments are the actual values passed to the function when it is called.
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
Placeholder question
|
||||
What happens if a function is called with more arguments than it expects?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
Placeholder distractor 1
|
||||
The extra arguments will be assigned to the remaining parameters.
|
||||
|
||||
---
|
||||
|
||||
Placeholder distractor 2
|
||||
The function will throw an error.
|
||||
|
||||
---
|
||||
|
||||
Placeholder distractor 3
|
||||
The function will use the default values specified in the function definition.
|
||||
|
||||
#### --answer--
|
||||
|
||||
Placeholder answer
|
||||
The extra arguments will be ignored.
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
Placeholder question
|
||||
What is the correct syntax for defining a function with default parameter values?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
Placeholder distractor 1
|
||||
`function myFunction(param1 = value1, param2) { ... }`
|
||||
|
||||
---
|
||||
|
||||
Placeholder distractor 2
|
||||
`function myFunction(param1, param2 = value2) { ... }`
|
||||
|
||||
---
|
||||
|
||||
Placeholder distractor 3
|
||||
`function myFunction(param1: value1, param2: value2) { ... }`
|
||||
|
||||
#### --answer--
|
||||
|
||||
Placeholder answer
|
||||
`function myFunction(param1 = value1, param2 = value2) { ... }`
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
Placeholder question
|
||||
What is the default value for a missing argument in a function call?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
Placeholder distractor 1
|
||||
`Error`
|
||||
|
||||
---
|
||||
|
||||
Placeholder distractor 2
|
||||
`0`
|
||||
|
||||
---
|
||||
|
||||
Placeholder distractor 3
|
||||
`null`
|
||||
|
||||
#### --answer--
|
||||
|
||||
Placeholder answer
|
||||
`undefined`
|
||||
|
||||
|
||||
Reference in New Issue
Block a user