mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-06-19 21:09:51 +08:00
refactor(curriculum): JavaScript Objects Quiz (#58597)
This commit is contained in:
parent
26390bee56
commit
f8679b85b2
@ -17,7 +17,7 @@ To pass the quiz, you must correctly answer at least 18 of the 20 questions belo
|
||||
|
||||
#### --text--
|
||||
|
||||
Which of the following does not describe an object in JavaScript?
|
||||
Which of the following does NOT describe an object in JavaScript?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
@ -29,11 +29,11 @@ A container for properties and methods.
|
||||
|
||||
---
|
||||
|
||||
One of JavaScript's data types.
|
||||
A non-primitive data type.
|
||||
|
||||
#### --answer--
|
||||
|
||||
One of JavaScript's primitives.
|
||||
A primitive data type.
|
||||
|
||||
### --question--
|
||||
|
||||
@ -43,58 +43,58 @@ Which of the following object definitions is invalid?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
`const obj = { key: "some-value" }`
|
||||
`const book = { "pages": 255 };`
|
||||
|
||||
---
|
||||
|
||||
`const obj = { "some-value": key }`
|
||||
`const car = { year: 2025 };`
|
||||
|
||||
---
|
||||
|
||||
`const obj = { key: () => "some-value" }`
|
||||
`const person = { "name": "nora" };`
|
||||
|
||||
#### --answer--
|
||||
|
||||
`const obj = { key(): "some-value" }`
|
||||
`const product = { color: blue };`
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
Which of the following correctly accesses the value of the `key` property in the object below:
|
||||
How can you access the `job` property of this `person` object?
|
||||
|
||||
```js
|
||||
const obj = { key: "some-value" }
|
||||
let person = { job: "Software Developer" };
|
||||
```
|
||||
|
||||
#### --distractors--
|
||||
|
||||
`obj -> key`
|
||||
`person -> job`
|
||||
|
||||
---
|
||||
|
||||
`obj#key`
|
||||
`person#job`
|
||||
|
||||
---
|
||||
|
||||
`obj[key]`
|
||||
`person[job]`
|
||||
|
||||
#### --answer--
|
||||
|
||||
`obj.key`
|
||||
`person.job`
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
Which of the following correctly accesses the value of the `street` property in the object below?
|
||||
How can you access the `street` property of the `person` object below?
|
||||
|
||||
```js
|
||||
const person = {
|
||||
address: {
|
||||
street: "sample-street"
|
||||
}
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
#### --distractors--
|
||||
@ -117,7 +117,7 @@ const person = {
|
||||
|
||||
#### --text--
|
||||
|
||||
Which of the following property keys can only be used with a bracket notation as opposed to dot notation?
|
||||
Which of these property keys can only be accessed using bracket notation?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
@ -125,7 +125,7 @@ A property key that is known and static.
|
||||
|
||||
---
|
||||
|
||||
A property key nested in multiple levels of the object.
|
||||
A property key that is nested within multiple levels of an object.
|
||||
|
||||
---
|
||||
|
||||
@ -139,7 +139,7 @@ A property key that is dynamic.
|
||||
|
||||
#### --text--
|
||||
|
||||
Which of the following correctly accesses the value of `name` property via destructuring in the object below?
|
||||
Which of the following correctly uses destructuring to access the `name` property of the `person` object below?
|
||||
|
||||
```js
|
||||
const person = { name: "John" }
|
||||
@ -147,219 +147,222 @@ const person = { name: "John" }
|
||||
|
||||
#### --distractors--
|
||||
|
||||
`const name = person.name`
|
||||
`const name = person.name;`
|
||||
|
||||
---
|
||||
|
||||
`const name = person["name"]`
|
||||
`const name = person["name"];`
|
||||
|
||||
---
|
||||
|
||||
`const { ...name } = person`
|
||||
`const { ...name } = person;`
|
||||
|
||||
#### --answer--
|
||||
|
||||
`const { name } = person`
|
||||
`const { name } = person;`
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
Which of the following correctly accesses the value of `street` property via destructuring in the object below?
|
||||
Which of the following correctly uses destructuring to access the `flour` property of the `recipe` object below?
|
||||
|
||||
```js
|
||||
const person = {
|
||||
address: {
|
||||
street: "sample-street"
|
||||
const recipe = {
|
||||
ingredients: {
|
||||
flour: "2 cups",
|
||||
sugar: "1 cup"
|
||||
}
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
#### --distractors--
|
||||
|
||||
`const name = person.address.street`
|
||||
`const flour = recipe.ingredients.flour;`
|
||||
|
||||
---
|
||||
|
||||
`const name = person["address"]["street"]`
|
||||
`const flour = recipe["ingredients"]["flour"];`
|
||||
|
||||
---
|
||||
|
||||
`const { address: street } = person`
|
||||
`const { ingredients: flour } = recipe;`
|
||||
|
||||
#### --answer--
|
||||
|
||||
`const { address: { street } } = person`
|
||||
`const { ingredients: { flour } } = recipe;`
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
How do you assign a default value of `"defaultValue"` to a property key accessed via destructuring?
|
||||
How can you assign a default value to a property key when using destructuring in JavaScript?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
`const key = object.key && "defaultValue"`
|
||||
`const key = object.key && "defaultValue";`
|
||||
|
||||
---
|
||||
|
||||
`const key = object.key || "defaultValue"`
|
||||
`const key = object.key || "defaultValue";`
|
||||
|
||||
---
|
||||
|
||||
`const { key: "defaultValue" } = object `
|
||||
`const { key: "defaultValue" } = object;`
|
||||
|
||||
#### --answer--
|
||||
|
||||
`const { key = "defaultValue" } = object`
|
||||
`const { key = "defaultValue" } = object;`
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
Which of the following objects does not inherit from `Object.prototype`?
|
||||
How can you check if a `car` object has a `year` property?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
`const obj = Object.create(undefined)`
|
||||
`year.hasProperty("car");`
|
||||
|
||||
---
|
||||
|
||||
`const obj = new Object(null)`
|
||||
`car.hasProperty("year");`
|
||||
|
||||
---
|
||||
|
||||
`const obj = { proto: undefined }`
|
||||
`car.hasOwnProperty(year);`
|
||||
|
||||
#### --answer--
|
||||
|
||||
`const obj = Object.create(null}`
|
||||
`car.hasOwnProperty("year");`
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
Which of the following cannot be an object prototype?
|
||||
What is an object method?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
`Math`
|
||||
A string associated with an object.
|
||||
|
||||
---
|
||||
|
||||
`Date`
|
||||
An array associated with an object.
|
||||
|
||||
---
|
||||
|
||||
`null`
|
||||
A number associated with an object.
|
||||
|
||||
#### --answer--
|
||||
|
||||
`undefined`
|
||||
A function associated with an object.
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
Which of the following correctly creates an object with the prototype below:
|
||||
How can you call the `add` method of this `calculator` object to add the numbers `10` and `7`?
|
||||
|
||||
```js
|
||||
const samplePrototype = {
|
||||
doSomething: () => {}
|
||||
}
|
||||
const calculator = {
|
||||
add: function(a, b) {
|
||||
return a + b;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
#### --distractors--
|
||||
|
||||
`const obj = { proto: samplePrototype }`
|
||||
`calculator#add(10, 7);`
|
||||
|
||||
---
|
||||
|
||||
`const obj = { prototype: samplePrototype }`
|
||||
`calculator.call(add(10, 7));`
|
||||
|
||||
---
|
||||
|
||||
`const obj = new Object(samplePrototype)`
|
||||
`add.calculator(10, 7);`
|
||||
|
||||
#### --answer--
|
||||
|
||||
`const obj = Object.create(samplePrototype)`
|
||||
`calculator.add(10, 7);`
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
Which of the following keywords is used to identify a getter in an Object?
|
||||
Which operator removes a property from an object?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
`getter`
|
||||
`remove`
|
||||
|
||||
---
|
||||
|
||||
`get_`
|
||||
`erase`
|
||||
|
||||
---
|
||||
|
||||
`_get`
|
||||
`discard`
|
||||
|
||||
#### --answer--
|
||||
|
||||
`get`
|
||||
`delete`
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
Which of the following keywords is used to identify a setter in an Object?
|
||||
How can you check if a `score` property exists in a `player` object?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
`setter`
|
||||
`player in "score"`
|
||||
|
||||
---
|
||||
|
||||
`set_`
|
||||
`player in score`
|
||||
|
||||
---
|
||||
|
||||
`_set`
|
||||
`score in player`
|
||||
|
||||
#### --answer--
|
||||
|
||||
`set`
|
||||
`"score" in player`
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
Which of the following is true about object setters?
|
||||
How can you create a new empty object in JavaScript?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
An object setter must be called with a parenthesis.
|
||||
`object() new`
|
||||
|
||||
---
|
||||
|
||||
An object setter must be defined with the same name as the property it sets.
|
||||
`new object()`
|
||||
|
||||
---
|
||||
|
||||
An object setter must be accompanied by a getter.
|
||||
`Object() new`
|
||||
|
||||
#### --answer--
|
||||
|
||||
An object setter must be defined with exactly one argument.
|
||||
`new Object()`
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
Which of the following is the correct syntax for optional chaining?
|
||||
What is the correct syntax for using optional chaining in JavaScript?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
`object.key`
|
||||
`object?key`
|
||||
|
||||
---
|
||||
|
||||
@ -377,109 +380,123 @@ Which of the following is the correct syntax for optional chaining?
|
||||
|
||||
#### --text--
|
||||
|
||||
What is the difference between enumerable and non-enumerable properties in an object?
|
||||
What is optional chaining used for in JavaScript?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
Enumerable properties are owned by the object while non-enumerable properties are not.
|
||||
Used to define variables with default values.
|
||||
|
||||
---
|
||||
|
||||
Enumerable properties are defined by the object while non-enumerable properties can only be inherited.
|
||||
Used to define optional methods within an object that may or may not be called.
|
||||
|
||||
---
|
||||
|
||||
Enumerable properties can be accessed via dot notation while non-enumerable properties can only be accessed via bracket notation.
|
||||
Used to write more concise code by chaining multiple method calls together in a single statement.
|
||||
|
||||
#### --answer--
|
||||
|
||||
Enumerable properties are properties whose internal enumerable flag is set to true while non-enumerable properties' internal enumerable flag is set to false.
|
||||
Used to safely access properties of an object that might be `null` or `undefined`.
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
What is the difference between `Object.keys()` and `Object.getOwnPropertyNames()`?
|
||||
What method is used to convert a JavaScript object into a JSON string?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
`Object.keys()` returns all the properties prefixed with the word `key_` while `Object.getOwnPropertyNames()` returns all object properties regardless of prefix.
|
||||
`Stringify.toJSON()`
|
||||
|
||||
---
|
||||
|
||||
`Object.keys()` returns all properties inherited and owned by the object while `Object.getOwnPropertyNames()` only returns properties owned by the object.
|
||||
`JSON.toObject()`
|
||||
|
||||
---
|
||||
|
||||
`Object.keys()` and `Object.getOwnPropertyNames()` will always return the same list of properties.
|
||||
`Object.toJSON()`
|
||||
|
||||
#### --answer--
|
||||
|
||||
`Object.keys()` returns only enumerable properties of an object while `Object.getOwnPropertyNames()` returns enumerable and non-enumerable properties except for those which use Symbol.
|
||||
`JSON.stringify()`
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
Which of the following is true for an object sealed with `Object.seal`?
|
||||
What method converts a JSON string back into a JavaScript object?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
Values of existing properties cannot be changed.
|
||||
`JSON.toObject()`
|
||||
|
||||
---
|
||||
|
||||
Values of existing properties can be changed but doing this will unseal the object.
|
||||
`JSON.convert()`
|
||||
|
||||
---
|
||||
|
||||
New properties can be added but existing properties can not be removed.
|
||||
`JSON.object()`
|
||||
|
||||
#### --answer--
|
||||
|
||||
New properties cannot be added and existing properties cannot be removed.
|
||||
`JSON.parse()`
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
Which of the following is true for an object frozen with `Object.freeze`?
|
||||
Which one of these options accesses the `color` property of a `toy` object using bracket notation?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
New properties can be added but existing properties can not be removed.
|
||||
`toy.color`
|
||||
|
||||
---
|
||||
|
||||
New properties can be added but doing this will unfreeze the object.
|
||||
`toy[color]`
|
||||
|
||||
---
|
||||
|
||||
Values of existing properties can be changed but doing this will unfreeze the object.
|
||||
`color[toy]`
|
||||
|
||||
#### --answer--
|
||||
|
||||
Values of existing properties cannot be changed.
|
||||
`toy["color"]`
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
What is the right way to completely remove a property from an object?
|
||||
How can you access the `home` phone of this `learner` object in JavaScript using dot notation?
|
||||
|
||||
```js
|
||||
const learner = {
|
||||
name: "Nora",
|
||||
age: 45,
|
||||
contact: {
|
||||
email: "nora@email.com",
|
||||
phone: {
|
||||
home: "123-456-7890",
|
||||
work: "098-765-4321"
|
||||
}
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
#### --distractors--
|
||||
|
||||
`Object.delete(object, propertyKey)`
|
||||
`learner.name.phone.home`
|
||||
|
||||
---
|
||||
|
||||
`remove object.propertyKey`
|
||||
`learner.email.work`
|
||||
|
||||
---
|
||||
|
||||
`object.propertyKey = undefined`
|
||||
`learner.contact.home`
|
||||
|
||||
#### --answer--
|
||||
|
||||
`delete object.propertyKey`
|
||||
`learner.contact.phone.home`
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user