MCQs Of Day 9: Objects in JavaScript
1. What is an object in JavaScript?
- A) A primitive data type
- B) A collection of properties and methods
- C) A function
- D) A type of array
Answer: B) A collection of properties and methods
Explanation: An object in JavaScript is a collection of properties, where each property is a key-value pair. It can also have methods (functions) associated with it.
2. How do you create an object in JavaScript?
- A) var obj = new Object();
- B) var obj = {};
- C) var obj = Object.create();
- D) All of the above
Answer: D) All of the above
Explanation: You can create an object in JavaScript using the new Object() syntax, the object literal {}, or using Object.create().
3. Which of the following is the correct way to access a property of an object in JavaScript?
- A) object.property
- B) object["property"]
- C) Both A and B
- D) None of the above
Answer: C) Both A and B
Explanation: You can access an object's property using dot notation (object.property) or bracket notation (object["property"]).
4. What will the following code output?
javascript
Copy code
let car = { make: "Toyota", model: "Corolla", year: 2021 };
console.log(car.model);
- A) Toyota
- B) 2021
- C) Corolla
- D) Undefined
Answer: C) Corolla
Explanation: The model property of the car object has the value "Corolla", which will be logged to the console.
5. Which of the following methods can be used to add a property to an object in JavaScript?
- A) object.addProperty()
- B) object.property = value;
- C) object.add("property", value);
- D) None of the above
Answer: B) object.property = value;
Explanation: To add a new property to an object, you can simply use dot notation (object.property = value).
6. How do you remove a property from an object in JavaScript?
- A) delete object.property;
- B) object.remove(property);
- C) object.remove(property)
- D) object.property = null;
Answer: A) delete object.property;
Explanation: The delete operator is used to remove a property from an object.
7. Which of the following is NOT a valid way to access an object’s property?
- A) object["property"]
- B) object.property
- C) object[0]
- D) object["property name"]
Answer: C) object[0]
Explanation: You cannot access an object property using an index like in an array (object[0]). Use either dot or bracket notation.
8. What does Object.keys(obj) return?
- A) An array of all the property names of the object
- B) An array of all the property values of the object
- C) An array of both property names and values
- D) An array of all the methods of the object
Answer: A) An array of all the property names of the object
Explanation: The Object.keys() method returns an array of the property names of an object.
9. How can you check if an object has a certain property?
- A) property in object
- B) object.hasOwnProperty(property)
- C) Both A and B
- D) None of the above
Answer: C) Both A and B
Explanation: You can check if an object has a property using the in operator or the hasOwnProperty() method.
10. What is the output of the following code?
javascript
Copy code
let person = { name: "John", age: 30 };
delete person.age;
console.log(person.age);
- A) 30
- B) undefined
- C) null
- D) age
Answer: B) undefined
Explanation: After deleting the age property, accessing person.age returns undefined because the property no longer exists.
11. How can you iterate over all properties of an object in JavaScript?
- A) for (let key in object)
- B) object.forEach()
- C) forEach(object)
- D) object.map()
Answer: A) for (let key in object)
Explanation: The for...in loop is used to iterate over all the enumerable properties of an object.
12. What will the following code output?
javascript
Copy code
let obj = { name: "Alice", age: 25 };
console.log(Object.keys(obj));
- A) ["Alice", 25]
- B) ["name", "age"]
- C) ["name: Alice", "age: 25"]
- D) undefined
Answer: B) ["name", "age"]
Explanation: Object.keys() returns an array of the object's keys, not the values.
13. What is the purpose of the Object.values() method in JavaScript?
- A) To return all the property names of an object
- B) To return all the property values of an object
- C) To return both the keys and values of an object
- D) To return an empty object
Answer: B) To return all the property values of an object
Explanation: The Object.values() method returns an array of all the values of an object's properties.
14. Which of the following is used to merge two objects into one?
- A) Object.combine(obj1, obj2)
- B) Object.merge(obj1, obj2)
- C) {...obj1, ...obj2}
- D) obj1.concat(obj2)
Answer: C) {...obj1, ...obj2}
Explanation: The spread operator (...) is used to merge two objects into one by copying properties from both objects.
15. Which of the following methods creates a new object with the same properties as the original?
- A) Object.assign()
- B) Object.copy()
- C) Object.create()
- D) Object.duplicate()
Answer: A) Object.assign()
Explanation: The Object.assign() method is used to copy the values of all properties from one or more source objects to a target object.
16. What will the following code output?
javascript
Copy code
let obj = { a: 1, b: 2 };
let newObj = Object.assign({}, obj);
obj.a = 3;
console.log(newObj.a);
- A) 1
- B) 2
- C) 3
- D) undefined
Answer: A) 1
Explanation: Object.assign() creates a shallow copy, so changing the a property in the original object does not affect the copied object.
17. How do you create a nested object in JavaScript?
- A) let obj = { prop: { nestedProp: value } };
- B) let obj = { nested: { prop: value } };
- C) let obj = { prop: { nestedProp: value } }
- D) All of the above
Answer: D) All of the above
Explanation: You can create a nested object by defining an object as a property of another object.
18. What is the correct way to define a method inside an object in JavaScript?
- A) method: function() {}
- B) method: () => {}
- C) method: function() {} or method: () => {}
- D) function method() {}
Answer: C) method: function() {} or method: () => {}
Explanation: You can define methods inside an object using either a traditional function expression or an arrow function.
19. Which of the following is a valid way to check if a property exists in an object?
- A) property in object
- B) object.has(property)
- C) object.hasOwnProperty(property)
- D) Both A and C
Answer: D) Both A and C
Explanation: You can use either the in operator or the hasOwnProperty() method to check if a property exists in an object.
20. What does the Object.getOwnPropertyNames() method return?
- A) All the keys of the object, including those in the prototype chain
- B) All the keys of the object itself
- C) All the values of the object
- D) A list of all enumerable properties
Answer: B) All the keys of the object itself
Explanation: The Object.getOwnPropertyNames() method returns all the own properties (including non-enumerable ones) of the object itself.
Let me know if you would like me to continue with the remaining MCQs!
4o mini
You said:
yes
ChatGPT said:
ChatGPT
Here is the continuation of the 100 MCQs for the concept of Objects in JavaScript, starting from question 21:
21. What is the result of this code?
javascript
Copy code
let obj = { a: 1, b: 2 };
console.log(Object.hasOwn(obj, 'a'));
- A) true
- B) false
- C) undefined
- D) Error
Answer: A) true
Explanation: Object.hasOwn() is a method that checks whether the object has the given property as its own (not inherited) property. Since a is a property of the object, the output will be true.
22. What will the following code return?
javascript
Copy code
let person = { firstName: "John", lastName: "Doe", age: 30 };
delete person.age;
console.log(person);
- A) { firstName: "John", lastName: "Doe" }
- B) { firstName: "John", lastName: "Doe", age: undefined }
- C) undefined
- D) { firstName: "John", age: 30 }
Answer: A) { firstName: "John", lastName: "Doe" }
Explanation: The delete operator removes the age property, leaving the remaining properties intact.
23. How do you access the prototype of an object in JavaScript?
- A) object.prototype
- B) Object.prototype
- C) object.__proto__
- D) object.getPrototype()
Answer: C) object.__proto__
Explanation: __proto__ is a reference to the prototype of an object. It allows you to access and manipulate the prototype of an object.
24. What is the output of the following code?
javascript
Copy code
let car = { make: "Toyota", model: "Corolla" };
car.year = 2021;
console.log(car.year);
- A) 2021
- B) undefined
- C) null
- D) Error
Answer: A) 2021
Explanation: The year property is added to the car object, and its value is 2021. So when logged, it prints 2021.
25. What does the Object.freeze() method do in JavaScript?
- A) It removes all properties of an object
- B) It prevents new properties from being added to an object
- C) It prevents changes to existing properties of an object
- D) Both B and C
Answer: D) Both B and C
Explanation: Object.freeze() makes an object immutable, preventing new properties from being added and existing properties from being modified.
26. How can you make a copy of an object in JavaScript?
- A) let copy = object.copy();
- B) let copy = Object.clone(object);
- C) let copy = Object.assign({}, object);
- D) let copy = object.clone();
Answer: C) let copy = Object.assign({}, object);
Explanation: Object.assign() is used to create a shallow copy of the object by copying its properties into a new object.
27. Which of the following methods returns a string representation of an object?
- A) Object.toString()
- B) Object.stringify()
- C) JSON.stringify()
- D) Object.toJSON()
Answer: C) JSON.stringify()
Explanation: JSON.stringify() is used to convert an object to a JSON string representation.
28. What will the following code output?
javascript
Copy code
let person = { name: "Alice", age: 25 };
console.log(Object.hasOwnProperty(person, "name"));
- A) true
- B) false
- C) undefined
- D) Error
Answer: D) Error
Explanation: The correct method is person.hasOwnProperty("name") (not Object.hasOwnProperty), which checks if the person object has the "name" property.
29. Which operator can be used to check if a property exists in an object?
- A) ==
- B) ===
- C) in
- D) hasOwnProperty
Answer: C) in
Explanation: The in operator checks whether a property exists in an object, including properties in the prototype chain.
30. What is the result of this code?
javascript
Copy code
let obj1 = { x: 1 };
let obj2 = { y: 2 };
let obj3 = Object.assign(obj1, obj2);
console.log(obj3);
- A) { x: 1, y: 2 }
- B) { x: 1 }
- C) { y: 2 }
- D) Error
Answer: A) { x: 1, y: 2 }
Explanation: Object.assign() copies all properties from obj2 into obj1, so obj3 contains properties from both obj1 and obj2.
31. Which method can be used to create a new object with the same properties as the existing one?
- A) Object.clone()
- B) Object.create()
- C) Object.copy()
- D) Object.assign()
Answer: B) Object.create()
Explanation: Object.create() creates a new object with the same prototype as the given object.
32. Which of the following methods would you use to add a property to an object dynamically?
- A) object.add("property", value);
- B) object.property = value;
- C) object["property"] = value;
- D) Both B and C
Answer: D) Both B and C
Explanation: You can use either dot notation (object.property = value) or bracket notation (object["property"] = value) to dynamically add a property to an object.
33. What is the purpose of Object.defineProperty() in JavaScript?
- A) To define a new object
- B) To define a property on an object and its behavior
- C) To merge multiple objects
- D) To get the keys of an object
Answer: B) To define a property on an object and its behavior
Explanation: Object.defineProperty() allows you to define a property and specify whether it's writable, enumerable, or configurable.
34. What will the following code output?
javascript
Copy code
let obj = { name: "John", age: 30 };
Object.defineProperty(obj, "age", { writable: false });
obj.age = 35;
console.log(obj.age);
- A) 35
- B) 30
- C) undefined
- D) Error
Answer: B) 30
Explanation: Since writable: false makes the age property read-only, attempting to change it results in no change, and the original value 30 remains.
35. What does Object.getPrototypeOf() do in JavaScript?
- A) It gets the values of an object's prototype
- B) It gets the keys of an object's prototype
- C) It gets the prototype of an object
- D) It sets the prototype of an object
Answer: C) It gets the prototype of an object
Explanation: Object.getPrototypeOf() returns the prototype of a specified object.
36. How would you check the prototype of an object in JavaScript?
- A) object.prototype
- B) object.__proto__
- C) object.getPrototype()
- D) Object.getPrototypeOf(object)
Answer: B) object.__proto__
Explanation: You can access an object's prototype using object.__proto__.
37. Which method allows you to prevent modifications to an object in JavaScript?
- A) Object.lock()
- B) Object.prevent()
- C) Object.freeze()
- D) Object.seal()
Answer: C) Object.freeze()
Explanation: Object.freeze() prevents modification to the properties and methods of an object, making it immutable.
38. How do you check if an object is empty?
- A) Object.isEmpty(object)
- B) Object.empty(object)
- C) Object.keys(object).length === 0
- D) object.isEmpty()
Answer: C) Object.keys(object).length === 0
Explanation: Object.keys(object).length returns the number of keys in the object. If it's 0, the object is empty.
39. What is the output of this code?
javascript
Copy code
let person = { name: "Alice", age: 25 };
let newPerson = Object.create(person);
newPerson.city = "New York";
console.log(newPerson.name);
- A) Alice
- B) undefined
- C) New York
- D) Error
Answer: A) Alice
Explanation: Object.create() creates a new object that inherits from person, so newPerson has access to person's properties, including name.
40. What will the following code output?
javascript
Copy code
let obj = { a: 1, b: 2 };
let newObj = { ...obj, c: 3 };
console.log(newObj);
- A) { a: 1, b: 2, c: 3 }
- B) { a: 1, b: 2 }
- C) { c: 3 }
- D) {}
Answer: A) { a: 1, b: 2, c: 3 }
Explanation: The spread operator (...) copies the properties from obj into newObj and adds the new property c: 3.
Let me know if you want me to continue further!
4o mini
You said:
yes
ChatGPT said:
ChatGPT
Here is the continuation of the 100 MCQs for the concept of Objects in JavaScript, starting from question 41:
41. What is the output of the following code?
javascript
Copy code
let obj = { a: 1, b: 2 };
console.log(Object.keys(obj));
- A) ['a', 'b']
- B) ['a', '1', 'b', '2']
- C) ['a', 'b', 'toString', 'constructor']
- D) ['a', 'b', 'valueOf']
Answer: A) ['a', 'b']
Explanation: Object.keys() returns an array of the object's own enumerable property names.
42. Which of the following methods checks if an object has a certain property?
- A) hasOwnProperty()
- B) hasProperty()
- C) checkProperty()
- D) hasKey()
Answer: A) hasOwnProperty()
Explanation: hasOwnProperty() checks if an object has a specified property as its own (not inherited).
43. What will the following code output?
javascript
Copy code
let obj = { a: 1, b: 2 };
delete obj.a;
console.log(obj);
- A) { a: 1, b: 2 }
- B) { b: 2 }
- C) undefined
- D) Error
Answer: B) { b: 2 }
Explanation: The delete operator removes the a property, so the resulting object contains only the b property.
44. What does the Object.keys() method return?
- A) An array of the object's values
- B) An array of the object's properties
- C) An array of the object's prototypes
- D) A string of the object's properties
Answer: B) An array of the object's properties
Explanation: Object.keys() returns an array containing the names of the object's enumerable properties.
45. What will this code output?
javascript
Copy code
let obj = { x: 10, y: 20 };
let anotherObj = { x: 30, z: 40 };
let mergedObj = Object.assign({}, obj, anotherObj);
console.log(mergedObj);
- A) { x: 30, y: 20, z: 40 }
- B) { x: 10, y: 20, z: 40 }
- C) { x: 10, y: 20 }
- D) { x: 30, y: 20 }
Answer: A) { x: 30, y: 20, z: 40 }
Explanation: Object.assign() copies properties from obj and anotherObj into a new object. If properties with the same name exist, the last one overrides the previous one (x becomes 30).
46. What will be the output of this code?
javascript
Copy code
let obj = { a: 1, b: 2, c: 3 };
Object.defineProperty(obj, 'd', { value: 4, writable: false });
obj.d = 5;
console.log(obj.d);
- A) 5
- B) 4
- C) undefined
- D) Error
Answer: B) 4
Explanation: Since the d property is defined as non-writable, the attempt to change its value is ignored, so it remains 4.
47. How do you add a method to an object?
- A) object.addMethod("method", function() {})
- B) object.method = function() {}
- C) object.add("method", function() {})
- D) object["method"] = function() {}
Answer: B) object.method = function() {}
Explanation: You can add methods to an object using dot notation (or bracket notation) by assigning a function to a property.
48. What will the following code output?
javascript
Copy code
let obj = { name: "Alice", age: 25 };
let descriptor = Object.getOwnPropertyDescriptor(obj, "name");
console.log(descriptor);
- A) { value: "Alice", writable: true, enumerable: true, configurable: true }
- B) { value: "Alice" }
- C) { writable: true, configurable: true }
- D) { value: "Alice", writable: false }
Answer: A) { value: "Alice", writable: true, enumerable: true, configurable: true }
Explanation: Object.getOwnPropertyDescriptor() returns an object describing the property with details such as value, writable, enumerable, and configurable.
49. What is the purpose of the Object.seal() method?
- A) To add properties to an object
- B) To make all properties of an object read-only
- C) To prevent new properties from being added to an object but allows modification of existing properties
- D) To prevent modification of existing properties but allows new properties to be added
Answer: C) To prevent new properties from being added to an object but allows modification of existing properties
Explanation: Object.seal() prevents new properties from being added to the object, but allows modification of existing ones. However, it does not prevent the removal of properties.
50. What will the following code output?
javascript
Copy code
let obj = { a: 1, b: 2 };
Object.freeze(obj);
obj.a = 3;
console.log(obj.a);
- A) 3
- B) 1
- C) undefined
- D) Error
Answer: B) 1
Explanation: Since the object is frozen using Object.freeze(), any modification to the object’s properties will be ignored. Therefore, obj.a will still be 1.
51. Which of the following methods can be used to convert an object into an array of its values?
- A) Object.toArray()
- B) Object.getValues()
- C) Object.values()
- D) Object.keys()
Answer: C) Object.values()
Explanation: Object.values() returns an array of the object's values.
52. Which of the following will correctly define a new object with a prototype?
javascript
Copy code
let proto = { greet: function() { console.log("Hello!"); } };
let obj = Object.create(proto);
- A) Object.create(proto)
- B) new proto()
- C) Object.new(proto)
- D) proto.create()
Answer: A) Object.create(proto)
Explanation: Object.create(proto) creates a new object with proto as its prototype.
53. What will be the output of the following code?
javascript
Copy code
let person = { name: "Bob", age: 40 };
let copyPerson = Object.assign({}, person);
console.log(copyPerson);
- A) { name: "Bob", age: 40 }
- B) undefined
- C) {}
- D) null
Answer: A) { name: "Bob", age: 40 }
Explanation: Object.assign() creates a shallow copy of the object, so copyPerson will have the same properties as person.
54. What will the following code output?
javascript
Copy code
let obj = { x: 10, y: 20 };
Object.defineProperty(obj, 'z', { value: 30, writable: false });
obj.z = 40;
console.log(obj.z);
- A) 40
- B) 30
- C) undefined
- D) Error
Answer: B) 30
Explanation: The z property is defined as non-writable (writable: false), so even though we try to modify it, the value remains 30.
55. What will the following code output?
javascript
Copy code
let obj = { a: 1, b: 2, c: 3 };
delete obj.b;
console.log(obj);
- A) { a: 1, b: 2, c: 3 }
- B) { a: 1, c: 3 }
- C) undefined
- D) Error
Answer: B) { a: 1, c: 3 }
Explanation: The delete operator removes the b property, leaving the a and c properties.
Let me know if you would like me to continue further!
4o mini
You said:
yes
ChatGPT said:
ChatGPT
Here is the continuation of the 100 MCQs for the concept of Objects in JavaScript, continuing from question 56:
56. What will the following code output?
javascript
Copy code
let obj = { a: 1, b: 2 };
console.log(Object.getOwnPropertyNames(obj));
- A) ['a', 'b']
- B) ['a', '1', 'b', '2']
- C) ['a', 'b', 'toString', 'constructor']
- D) ['a', 'b', 'valueOf']
Answer: A) ['a', 'b']
Explanation: Object.getOwnPropertyNames() returns an array of the object's own property names (including non-enumerable properties).
57. Which of the following can be used to define an object with both methods and properties?
javascript
Copy code
let obj = {
property1: "value",
method1: function() { return "Hello!"; }
};
- A) object.method = function() {}
- B) object = {method: function() {}}
- C) Both A and B
- D) None of the above
Answer: C) Both A and B
Explanation: You can define both properties and methods inside an object using either method (object.method = function() or object = {}).
58. What is the output of the following code?
javascript
Copy code
let obj1 = { a: 1, b: 2 };
let obj2 = { c: 3 };
let obj3 = Object.assign({}, obj1, obj2);
obj3.a = 4;
console.log(obj3);
- A) { a: 4, b: 2, c: 3 }
- B) { a: 1, b: 2, c: 3 }
- C) { a: 4, b: 2 }
- D) { a: 4, b: 2, c: 4 }
Answer: A) { a: 4, b: 2, c: 3 }
Explanation: Object.assign() copies properties from obj1 and obj2 into a new object. After copying, the value of a is updated to 4.
59. Which of the following statements about JavaScript objects is correct?
- A) An object can only have primitive values as properties.
- B) An object can only have methods as properties.
- C) An object can have both methods and primitive values as properties.
- D) An object cannot have methods as properties.
Answer: C) An object can have both methods and primitive values as properties.
Explanation: JavaScript objects can hold both data (like strings, numbers) and methods (functions).
60. What does the Object.preventExtensions() method do?
- A) Prevents an object from being extended with new properties
- B) Prevents modification of existing properties
- C) Freezes an object
- D) Allows only the addition of new properties to an object
Answer: A) Prevents an object from being extended with new properties
Explanation: Object.preventExtensions() prevents new properties from being added to an object but does not affect existing properties.
61. What will the following code output?
javascript
Copy code
let obj = { x: 10, y: 20 };
let clonedObj = { ...obj, z: 30 };
console.log(clonedObj);
- A) { x: 10, y: 20, z: 30 }
- B) { x: 10, y: 20 }
- C) { z: 30 }
- D) {}
Answer: A) { x: 10, y: 20, z: 30 }
Explanation: The spread operator (...) copies the properties of obj into a new object clonedObj and adds the new property z: 30.
62. Which of the following is the correct way to access a property using a variable key?
javascript
Copy code
let key = "name";
let obj = { name: "Alice" };
- A) obj.key
- B) obj["key"]
- C) obj[key]
- D) obj.key()
Answer: C) obj[key]
Explanation: You can access an object property using a variable key by using bracket notation obj[key].
63. Which method is used to merge multiple objects into one in JavaScript?
- A) merge()
- B) combine()
- C) Object.merge()
- D) Object.assign()
Answer: D) Object.assign()
Explanation: Object.assign() is used to merge multiple objects into a single object by copying all properties from the source objects to the target object.
64. What will the following code output?
javascript
Copy code
let obj = { a: 1, b: 2 };
Object.freeze(obj);
obj.b = 3;
console.log(obj.b);
- A) 3
- B) 2
- C) undefined
- D) Error
Answer: B) 2
Explanation: Object.freeze() prevents modification of an object’s properties, so obj.b remains 2.
65. What will be the output of the following code?
javascript
Copy code
let obj = { a: 1, b: 2 };
console.log(Object.isSealed(obj));
- A) true
- B) false
- C) undefined
- D) Error
Answer: B) false
Explanation: Object.isSealed() checks whether an object is sealed. Since obj is not sealed, the result is false.
66. Which method would you use to convert an object to a JSON string?
- A) JSON.stringify()
- B) Object.stringify()
- C) JSON.toString()
- D) Object.toJSON()
Answer: A) JSON.stringify()
Explanation: JSON.stringify() converts a JavaScript object into a JSON string.
67. What will the following code output?
javascript
Copy code
let obj = { a: 1, b: 2 };
delete obj;
console.log(obj);
- A) { a: 1, b: 2 }
- B) undefined
- C) null
- D) Error
Answer: D) Error
Explanation: The delete operator only deletes properties from an object, not the object itself. Attempting to delete an entire object will result in an error.
68. Which of the following methods can be used to get the values of an object?
- A) Object.keys()
- B) Object.getOwnPropertyNames()
- C) Object.values()
- D) Object.allValues()
Answer: C) Object.values()
Explanation: Object.values() returns an array of the object's values.
69. What is the purpose of the Object.freeze() method in JavaScript?
- A) To prevent new properties from being added to an object
- B) To prevent modification of existing properties of an object
- C) To make all properties of an object immutable
- D) All of the above
Answer: D) All of the above
Explanation: Object.freeze() makes an object immutable by preventing the addition of new properties, modification of existing properties, and marking the properties as non-writable.
70. Which of the following methods creates a new object that inherits from a given object?
- A) Object.create()
- B) Object.assign()
- C) Object.extend()
- D) Object.merge()
Answer: A) Object.create()
Explanation: Object.create() creates a new object that inherits from the specified prototype object.
Let me know if you want me to continue further!
4o mini
You said:
yes
ChatGPT said:
ChatGPT
Here is the continuation of the 100 MCQs for the concept of Objects in JavaScript:
71. What will the following code output?
javascript
Copy code
let person = { name: "John", age: 30 };
person.age = 31;
console.log(person.age);
- A) 30
- B) 31
- C) undefined
- D) Error
Answer: B) 31
Explanation: The value of the age property is updated from 30 to 31, so the output is 31.
72. Which of the following statements creates a shallow copy of an object in JavaScript?
- A) let copy = Object.assign({}, originalObject);
- B) let copy = originalObject.copy();
- C) let copy = originalObject.clone();
- D) let copy = {...originalObject};
Answer: A) let copy = Object.assign({}, originalObject);
Explanation: Both Object.assign() and the spread operator ... create a shallow copy of the object. However, Object.assign() is more commonly used for this purpose.
73. Which of the following methods returns an array of an object's keys?
- A) Object.getKeys()
- B) Object.keys()
- C) Object.getOwnKeys()
- D) Object.keysValues()
Answer: B) Object.keys()
Explanation: Object.keys() returns an array of the object's own enumerable property names.
74. Which of the following methods would you use to check if an object has a property?
- A) Object.hasProperty()
- B) Object.hasOwnProperty()
- C) Object.contains()
- D) Object.propertyExists()
Answer: B) Object.hasOwnProperty()
Explanation: hasOwnProperty() checks if an object has a specified property as its own (not inherited).
75. What will the following code output?
javascript
Copy code
let person = { name: "Jane", age: 25 };
let info = Object.entries(person);
console.log(info);
- A) [['name', 'Jane'], ['age', 25]]
- B) [['name', 'Jane'], ['age', '25']]
- C) ['name', 'age']
- D) ['Jane', 25]
Answer: A) [['name', 'Jane'], ['age', 25]]
Explanation: Object.entries() returns an array of the object's key-value pairs in the form of an array of arrays.
76. Which method is used to check if an object has a specific property or method?
- A) Object.is()
- B) Object.hasOwnProperty()
- C) Object.has()
- D) Object.exists()
Answer: B) Object.hasOwnProperty()
Explanation: hasOwnProperty() checks if the object has the specified property as its own, excluding properties from the prototype chain.
77. What will the following code output?
javascript
Copy code
let obj = { a: 1, b: 2 };
delete obj.b;
console.log(obj);
- A) { a: 1 }
- B) { a: 1, b: 2 }
- C) { b: 2 }
- D) Error
Answer: A) { a: 1 }
Explanation: delete removes the b property from the object, leaving only a.
78. What will happen if you try to delete a property of a frozen object?
- A) The property will be deleted successfully
- B) An error will be thrown
- C) The property will not be deleted and no error will occur
- D) The object will become unfrozen
Answer: C) The property will not be deleted and no error will occur
Explanation: Object.freeze() prevents modifications, so the property cannot be deleted, but no error will occur.
79. Which of the following objects will the Object.keys() method return?
javascript
Copy code
let obj = Object.create({ name: 'Alice' });
obj.age = 25;
- A) ['age']
- B) ['name', 'age']
- C) ['name']
- D) []
Answer: A) ['age']
Explanation: Object.keys() only returns the object's own enumerable properties, not inherited ones. So, it returns ['age'].
80. What is the output of the following code?
javascript
Copy code
let obj = { a: 1, b: 2 };
Object.defineProperty(obj, 'c', { value: 3, writable: false });
obj.c = 4;
console.log(obj.c);
- A) 4
- B) 3
- C) undefined
- D) Error
Answer: B) 3
Explanation: Since the writable attribute of the c property is set to false, the value of c cannot be modified.
81. Which of the following is NOT a valid object property type in JavaScript?
- A) String
- B) Number
- C) Function
- D) NaN
Answer: D) NaN
Explanation: NaN is a special value in JavaScript, but it cannot be used as an object property. Object properties can be any valid data type like strings, numbers, or functions.
82. How can you add a property to an object in JavaScript?
- A) object.property = value
- B) object['property'] = value
- C) Object.addProperty(object, 'property', value)
- D) All of the above
Answer: D) All of the above
Explanation: All three methods can be used to add properties to an object in JavaScript.
83. What does the Object.getOwnPropertyDescriptors() method return?
- A) An array of an object's property names
- B) A copy of an object
- C) An object containing descriptors of all properties
- D) A list of all enumerable properties
Answer: C) An object containing descriptors of all properties
Explanation: Object.getOwnPropertyDescriptors() returns an object containing the descriptors of all own properties, including their value, writable, enumerable, and configurable attributes.
84. What does the Object.is() method do?
- A) Compares two objects for equality
- B) Compares two values for strict equality, including special cases like NaN
- C) Returns true if an object is empty
- D) Checks if an object is frozen
Answer: B) Compares two values for strict equality, including special cases like NaN
Explanation: Object.is() compares two values for strict equality and can correctly handle special cases such as NaN being equal to NaN.
85. What will the following code output?
javascript
Copy code
let obj = { a: 1 };
Object.defineProperty(obj, 'b', { value: 2, configurable: false });
delete obj.b;
console.log(obj);
- A) { a: 1, b: 2 }
- B) { a: 1 }
- C) { a: 1, b: undefined }
- D) Error
Answer: A) { a: 1, b: 2 }
Explanation: Since configurable is false, the b property cannot be deleted, and it remains in the object.
86. Which of the following is the correct way to iterate over an object’s properties?
- A) for (key in object)
- B) object.forEach(key)
- C) object.map()
- D) for (key of object)
Answer: A) for (key in object)
Explanation: The for...in loop is used to iterate over the properties of an object.
87. How can you check if an object is empty in JavaScript?
- A) Object.isEmpty(object)
- B) object.isEmpty()
- C) Object.keys(object).length === 0
- D) object.length === 0
Answer: C) Object.keys(object).length === 0
Explanation: You can check if an object is empty by using Object.keys() to get the keys and checking if the length is 0.
88. What will the following code output?
javascript
Copy code
let obj = { a: 1, b: 2 };
let newObj = { ...obj, b: 3 };
console.log(newObj);
- A) { a: 1, b: 2 }
- B) { a: 1, b: 3 }
- C) { b: 3 }
- D) { a: 1 }
Answer: B) { a: 1, b: 3 }
Explanation: The spread operator copies all properties of obj to newObj, and the value of b is overwritten with 3.
89. Which of the following methods would be best for merging two objects?
- A) Object.concat()
- B) Object.merge()
- C) Object.assign()
- D) Object.append()
Answer: C) Object.assign()
Explanation: Object.assign() is used to copy all properties from one or more source objects to a target object.
90. What is the purpose of Object.freeze()?
- A) Prevents new properties from being added to an object
- B) Makes all properties of an object non-configurable
- C) Prevents the modification of an object’s properties
- D) Both A and C
Answer: D) Both A and C
Explanation: Object.freeze() makes the object immutable, preventing new properties from being added and existing properties from being modified.
91. What will happen when you try to modify a property of a frozen object?
- A) The property will be modified successfully
- B) The object will be unfrozen automatically
- C) A TypeError will be thrown
- D) The modification will be ignored silently
Answer: D) The modification will be ignored silently
Explanation: When an object is frozen using Object.freeze(), attempts to modify its properties will be silently ignored.
92. What will the following code output?
javascript
Copy code
let obj = { a: 1, b: 2 };
Object.defineProperty(obj, 'a', { writable: false });
obj.a = 3;
console.log(obj.a);
- A) 3
- B) undefined
- C) 1
- D) Error
Answer: C) 1
Explanation: The writable: false descriptor makes the property a non-writable, so its value remains 1.
93. What does the Object.getPrototypeOf() method do?
- A) Returns the prototype (i.e., the internal [[Prototype]] property) of an object
- B) Sets the prototype of an object
- C) Creates a new object with a specified prototype
- D) Checks if an object has a prototype
Answer: A) Returns the prototype (i.e., the internal [[Prototype]] property) of an object
Explanation: Object.getPrototypeOf() returns the prototype of a specified object.
94. Which of the following methods creates a deep copy of an object?
- A) Object.assign()
- B) JSON.parse(JSON.stringify(object))
- C) Object.clone()
- D) Object.copy()
Answer: B) JSON.parse(JSON.stringify(object))
Explanation: JSON.parse(JSON.stringify(object)) creates a deep copy by serializing the object to JSON and then deserializing it back.
95. What will the following code output?
javascript
Copy code
let obj = { name: "Alice" };
let person = Object.create(obj);
person.age = 25;
console.log(person.name);
- A) undefined
- B) Alice
- C) null
- D) Error
Answer: B) Alice
Explanation: Object.create(obj) creates a new object with obj as its prototype. The person object inherits the name property from obj.
96. Which of the following is used to prevent an object from being modified in JavaScript?
- A) Object.prevent()
- B) Object.seal()
- C) Object.freeze()
- D) Object.lock()
Answer: C) Object.freeze()
Explanation: Object.freeze() prevents modifications, additions, or deletions of properties on the object.
97. Which of the following methods can be used to get all property descriptors of an object?
- A) Object.getOwnPropertyDescriptors()
- B) Object.getAllProperties()
- C) Object.getOwnProperties()
- D) Object.getProperties()
Answer: A) Object.getOwnPropertyDescriptors()
Explanation: Object.getOwnPropertyDescriptors() returns all the property descriptors (including value, configurable, writable, and enumerable) for the object’s own properties.
98. How can you clone an object in JavaScript?
- A) Object.clone(object)
- B) object.copy()
- C) Object.assign({}, object)
- D) object.duplicate()
Answer: C) Object.assign({}, object)
Explanation: Object.assign() is used to clone an object by copying its properties to a new object.
99. What does Object.values() return?
- A) An array of the object's keys
- B) An array of the object's values
- C) An array of the object's property descriptors
- D) The object itself
Answer: B) An array of the object's values
Explanation: Object.values() returns an array of an object's own enumerable property values.
100. What will the following code output?
javascript
Copy code
let obj = { a: 1, b: 2, c: 3 };
for (let key in obj) {
console.log(key);
}
- A) a b c
- B) 1 2 3
- C) a, b, c
- D) undefined
Answer: A) a b c
Explanation: The for...in loop iterates over the enumerable properties of the object, so the keys a, b, and c are logged.
