Master Chapter 18: Advanced Object Features with These 30 MCQs Dive into Chapter 18 and boost your expertise in JavaScript object techniques. Explore Object Destructuring, Spread and Rest Operators, Object Property Shorthand, Computed Properties, and the utility methods Object.keys, Object.values, and Object.entries. Test your knowledge now!
Object Destructuring (MCQs 1-8)
What is the correct syntax for destructuring properties from an object?
A) const {name, age} = object;
B) let object = {name, age};
C) const object = [name, age];
D) let {name, age} = object[];
In the code const {x, y} = {x: 10, y: 20};, what is the value of y?
A) undefined
B) null
C) 10
D) 20
Which syntax allows renaming variables while destructuring?
A) const {x, y: z} = obj;
B) const {x, y = z} = obj;
C) let {x as y} = obj;
D) let obj = {x: y};
How do you assign default values during destructuring?
A) const {a = 5} = obj;
B) let {a: 5} = obj;
C) const obj = {a || 5};
D) let obj[a] = 5;
In nested destructuring, how would you access a nested property?
A) const {user: {name}} = data;
B) const {name.user} = data;
C) const name = user.data;
D) let data = {name.user};
What happens if a destructured property does not exist in the object?
A) Throws an error
B) Undefined is returned
C) Default value is mandatory
D) Value becomes null
Which is invalid destructuring syntax?
A) const {x, y} = obj;
B) let {x, ...y} = obj;
C) let {x: y} = obj;
D) const [x, y] = obj;
Can destructuring be used in function parameters?
A) Yes, with objects
B) No, only for arrays
C) Only if default values are present
D) No, destructuring is not allowed
Spread and Rest Operators (MCQs 9-16)
Which symbol represents the spread operator in JavaScript?
A) ...
B) ,,
C) ::
D) --
How can you merge two objects using the spread operator?
A) const obj3 = {...obj1, ...obj2};
B) let obj3 = [obj1, obj2];
C) let obj3 = obj1 + obj2;
D) const obj3 = obj1.concat(obj2);
What does the rest operator do?
A) Merges objects
B) Gathers remaining elements into an array
C) Extracts specific elements
D) Combines keys and values
Which of the following is valid syntax for the rest operator?
A) const {...rest} = obj;
B) let obj = {rest...};
C) let rest = {...obj};
D) const obj = {..., rest};
What will const [first, ...rest] = [1, 2, 3]; return for rest?
A) [1, 2, 3]
B) [2, 3]
C) [1]
D) []
How can you copy an object using the spread operator?
A) const copy = {...original};
B) const copy = {original};
C) let copy = [...original];
D) let copy = {original...};
Can the spread operator be used with strings?
A) Yes, it splits characters
B) No, only arrays
C) Only for concatenation
D) It works with objects, not strings
Which of these is NOT a valid use case for the rest operator?
A) Gathering function arguments
B) Copying arrays
C) Combining multiple objects
D) Filtering remaining elements
Object Property Shorthand and Computed Properties (MCQs 17-24)
What does the property shorthand allow?
A) Simplifies object creation when property names match variables
B) Creates default values
C) Automatically generates keys
D) Reassigns variables
Which is a valid shorthand property?
A) const obj = {name, age};
B) const obj = {name: name, age};
C) let obj = {name: "name", age};
D) let obj = {"name": name, age};
What does a computed property key look like?
A) const obj = {[key]: value};
B) const obj = {key: value};
C) let obj = {key[]: value};
D) const obj = {value[key]};
Can computed properties include expressions?
A) Yes, inside square brackets
B) No, only static strings are allowed
C) Only if they are integers
D) Yes, but only as values
What will this output: const obj = {[1+2]: 'value'};?
A) {3: 'value'}
B) {'3': 'value'}
C) {[1+2]: 'value'}
D) Throws an error
Which is NOT true for computed properties?
A) They allow dynamic keys
B) They require ES6+ syntax
C) Only one computed property is allowed per object
D) Can use variables as keys
In property shorthand, how do you assign a variable to a property?
A) Use the variable name directly
B) Use the => operator
C) Wrap the variable in square brackets
D) Assign using = key
What is the benefit of using shorthand properties?