Multiple Choice Question for Grand JS Quiz



(Chap # 61 - The DOM - Junk artifacts and nodeType.)

(1) What does the nodeType property return for an element node?

A) 1
B) 2
C) 3
D) 0

Answer: A) 1

Explanation: The nodeType property returns 1 for element nodes, 3 for text nodes, and 8 for comment nodes.



(2) What does the nodeType value 3 represent in the DOM?

A) Element node
B) Text node
C) Comment node
D) Document node

Answer: B) Text node

Explanation: A nodeType of 3 corresponds to text nodes, which often include whitespace or actual text.



(3) Why do junk artifacts like whitespace matter in the DOM?

A) They increase download speed
B) They simplify code
C) They are ignored by browsers
D) They can affect DOM navigation

Answer: D) They can affect DOM navigation

Explanation: Whitespace and text nodes can interfere with child node indexing and traversal.



(4) What is the typical nodeType value for a comment in the DOM?

A) 0
B) 8
C) 2
D) 4

Answer: B) 8

Explanation: Comment nodes in the DOM have a nodeType of 8.



(5) Which of the following code snippets correctly checks if a node is an element node?

A) if (node.nodeType === 3)
B) if (node.nodeType === 1)
C) if (node.nodeType == "element")
D) if (node.type === 1)

Answer: B) if (node.nodeType === 1)

Explanation: The nodeType for element nodes is 1, and it should be compared using strict equality.



(6) What is returned by someElement.firstChild.nodeType if the element starts with a text node?

A) 1
B) 3
C) 0
D) 8

Answer: B) 3

Explanation: If the first child of an element is a text node (including whitespace), firstChild.nodeType will return 3.



(7) Which of the following node types is most likely to cause confusion during DOM traversal?

A) Element
B) Comment
C) Text
D) Attribute

Answer: C) Text

Explanation: Text nodes, especially whitespace, can be unexpected and cause issues with childNode indexing.



(8) What is the purpose of checking nodeType before manipulating a node?

A) To avoid security risks
B) To make sure the node is not read-only
C) To ensure correct operations on intended node types
D) To verify the node is clickable

Answer: C) To ensure correct operations on intended node types

Explanation: By checking nodeType, developers avoid errors like trying to manipulate text nodes as if they were element nodes.



(9) What does node.nodeType === 8 evaluate?

A) Whether node is a text node
B) Whether node is a comment
C) Whether node is a tag
D) Whether node is null

Answer: B) Whether node is a comment

Explanation: nodeType 8 refers to comment nodes in the DOM structure.



(10) Which method can skip text nodes and directly target element nodes only?

A) getElementById()
B) getElementsByTagName()
C) querySelectorAll()
D) All of the above

Answer: D) All of the above

Explanation: These methods return only element nodes, automatically ignoring text and comment nodes.



(Chap # 62 - The DOM – More ways to target elements)

(1) What does document.getElementsByClassName("myClass") return?

A) A single element
B) A NodeList
C) An HTMLCollection
D) A string

Answer: C) An HTMLCollection

Explanation: The method returns a live HTMLCollection of all elements with the specified class name.



(2) Which method returns only the first matching element?

A) querySelector()
B) querySelectorAll()
C) getElementsByTagName()
D) getElementsByClassName()

Answer: A) querySelector()

Explanation: querySelector() returns the first element that matches a specified CSS selector.



(3) Which of the following methods return a static NodeList?

A) getElementsByClassName()
B) getElementsByTagName()
C) querySelectorAll()
D) getElementById()

Answer: C) querySelectorAll()

Explanation: querySelectorAll() returns a static NodeList, unaffected by later DOM changes.



(4) How do you select all <p> elements in the DOM?

A) document.querySelectorAll("p")
B) document.getElementsByTagName("p")
C) Both A and B
D) None of the above

Answer: C) Both A and B

Explanation: Both methods retrieve all paragraph elements, though one returns a static list, the other live.



(5) What type of selector can you pass to querySelector()?

A) ID selector
B) Class selector
C) Tag selector
D) All of the above

Answer: D) All of the above

Explanation: querySelector() accepts any valid CSS selector, including ID, class, tag, and complex selectors.



(6) What does document.querySelectorAll(".item") return?

A) HTMLCollection
B) NodeList
C) Array
D) String

Answer: B) NodeList

Explanation: This method returns a static NodeList of all elements with the class .item.



(7) How do you target the element with ID "header" using a CSS selector?

A) querySelector("header")
B) querySelector("#header")
C) querySelector(".header")
D) querySelector("@header")

Answer: B) querySelector("#header")

Explanation: The hash symbol (#) is used to select elements by ID in CSS selectors.



(8) What’s the main difference between getElementsByClassName and querySelectorAll?

A) One returns a NodeList, the other an Array
B) One is live, the other static
C) One uses jQuery
D) There is no difference

Answer: B) One is live, the other static

Explanation: getElementsByClassName() returns a live collection, while querySelectorAll() returns a static NodeList.



(9) How do you convert a NodeList into an array?

A) Array.from()
B) toArray()
C) slice()
D) NodeList.array()

Answer: A) Array.from()

Explanation: Array.from() is the recommended way to convert NodeLists into real arrays.



(10) Which is true about querySelectorAll("ul li")?

A) It selects all <ul> tags
B) It selects all direct children only
C) It selects all <li> inside any <ul>
D) It selects no elements

Answer: C) It selects all <li> inside any <ul>

Explanation: The selector matches all <li> elements that are descendants of any <ul>.



(Chap # 63 - The DOM – Getting a target's name)

(1) What does the nodeName property return?

A) The tag name in lowercase
B) The text content of the element
C) The name of the node in uppercase
D) The ID of the node

Answer: C) The name of the node in uppercase

Explanation: nodeName returns the name of the node in uppercase for element nodes (e.g., "DIV", "P").



(2) What will element.nodeName return for a <p> tag?

A) p
B) para
C) "P"
D) <p>

Answer: C) "P"

Explanation: The nodeName of a <p> element is "P", as it's always returned in uppercase.



(3) What is the nodeName of a text node?

A) #TEXT
B) textNode
C) TEXT
D) txt

Answer: A) #TEXT

Explanation: Text nodes return #TEXT as their nodeName.



(4) What is the purpose of nodeName in DOM manipulation?

A) To get the element's text
B) To return the node's type
C) To identify the kind of node being dealt with
D) To return the value of an attribute

Answer: C) To identify the kind of node being dealt with

Explanation: nodeName helps identify what kind of node you're working with (e.g., "DIV", "#TEXT").



(5) Which of the following is true about the value of nodeName?

A) It's always lowercase
B) It's a number
C) It's always uppercase for element nodes
D) It's the same as innerHTML

Answer: C) It's always uppercase for element nodes

Explanation: The nodeName of element nodes is always returned in uppercase letters.



(6) How can you check if a node is a text node using nodeName?

A) Compare it to "text"
B) Compare it to "#TEXT"
C) Compare it to "TXT"
D) Use getNodeType()

Answer: B) Compare it to "#TEXT"

Explanation: To confirm a text node, compare its nodeName to "#TEXT".



(7) What would document.body.nodeName return?

A) "body"
B) "BODY"
C) "document"
D) "html"

Answer: B) "BODY"

Explanation: document.body targets the body element, and nodeName returns "BODY".



(8) If element.nodeName === "#TEXT", what should you avoid?

A) Changing its attributes
B) Appending a child
C) Setting innerHTML
D) All of the above

Answer: D) All of the above

Explanation: Text nodes cannot have attributes, children, or innerHTML.



(9) Which of these is a valid use case for nodeName?

A) Checking tag type before applying changes
B) Setting text content
C) Comparing CSS styles
D) Changing element ID

Answer: A) Checking tag type before applying changes

Explanation: nodeName helps determine an element's type before manipulation.



(10) Which nodeName value would an HTML comment return?

A) "COMMENT"
B) "#COMMENT"
C) "!--"
D) "#comment"

Answer: B) "#COMMENT"

Explanation: HTML comment nodes have the nodeName value of "#COMMENT".



(Chap # 64 - The DOM - counting elements)

(1) What method is commonly used to count all elements of a certain tag?

A) document.querySelector()
B) document.getElementById()
C) document.getElementsByTagName().length
D) document.countElements()

Answer: C) document.getElementsByTagName().length

Explanation: getElementsByTagName() returns a live HTMLCollection, and you can count elements using its length.



(2) How would you count all paragraph elements in a document?

A) document.getElementsByClassName("p").length
B) document.querySelectorAll("p")
C) document.getElementsByTagName("p").length
D) document.p.length

Answer: C) document.getElementsByTagName("p").length

Explanation: To count <p> tags, use getElementsByTagName("p").length.



(3) What does document.getElementsByTagName("li").length return?

A) Number of unordered lists
B) Number of list items
C) Number of nested lists
D) A list of list items

Answer: B) Number of list items

Explanation: getElementsByTagName("li") targets all <li> elements.



(4) Which of the following returns a **live** collection?

A) querySelectorAll()
B) getElementsByTagName()
C) Array.from()
D) getElementById()

Answer: B) getElementsByTagName()

Explanation: The result of getElementsByTagName() updates automatically with DOM changes.



(5) How would you store all <div> elements into a variable?

A) let divs = document.getElementByTagName("div")
B) let divs = document.getElementsByClass("div")
C) let divs = document.getElementsByTagName("div")
D) let divs = document.divs

Answer: C) let divs = document.getElementsByTagName("div")

Explanation: This correctly stores all <div> elements into a variable.



(6) What will document.getElementsByTagName("*").length return?

A) 0
B) Number of all elements in the document
C) Number of visible elements
D) Number of head and body tags only

Answer: B) Number of all elements in the document

Explanation: The asterisk * is a wildcard that selects all tags.



(7) Which method would you use to count all elements with a specific class?

A) getElementsByName()
B) getElementByClass()
C) getElementsByClassName().length
D) countByClass()

Answer: C) getElementsByClassName().length

Explanation: getElementsByClassName() targets elements with the given class.



(8) Which returns a **static** NodeList?

A) getElementsByTagName()
B) getElementsByClassName()
C) querySelectorAll()
D) getElementById()

Answer: C) querySelectorAll()

Explanation: querySelectorAll() returns a static list unaffected by DOM changes.



(9) What type of object does getElementsByTagName return?

A) NodeList
B) HTMLCollection
C) Array
D) Object

Answer: B) HTMLCollection

Explanation: getElementsByTagName() returns a live HTMLCollection.



(10) How can you convert an HTMLCollection to an array?

A) Array.from(collection)
B) collection.toArray()
C) collection.makeArray()
D) convertToArray(collection)

Answer: A) Array.from(collection)

Explanation: Array.from() is the standard way to convert HTMLCollections to arrays.



(Chap # 65 - The DOM - Attributes)

(1) What method is used to retrieve the value of an attribute from an element?

A) element.getAttr()
B) element.getAttribute()
C) element.attributeValue()
D) element.fetchAttribute()

Answer: B) element.getAttribute()

Explanation: getAttribute() is the standard method to retrieve the value of an attribute.



(2) How do you get the "href" of a link with id "myLink"?

A) document.getElementById("myLink").href
B) document.getElementById("myLink").getAttribute("href")
C) Both A and B
D) None of these

Answer: C) Both A and B

Explanation: You can either access the property directly or use getAttribute() to get the value.



(3) Which method is used to set or change an element's attribute value?

A) setAttr()
B) changeAttribute()
C) setAttribute()
D) modifyAttr()

Answer: C) setAttribute()

Explanation: setAttribute() lets you assign a new value to an attribute.



(4) What will this code do?
img.setAttribute("src", "new.jpg");


A) Gets the current image
B) Deletes the image
C) Changes the image source
D) Creates a new image element

Answer: C) Changes the image source

Explanation: This sets a new value for the src attribute of the image.



(5) What type of value does getAttribute() return?

A) Boolean
B) Integer
C) String
D) Object

Answer: C) String

Explanation: All values returned by getAttribute() are strings, even if numeric.



(6) What will happen if you try to get a non-existent attribute?

A) Error
B) null
C) undefined
D) The browser crashes

Answer: B) null

Explanation: getAttribute() returns null if the attribute doesn't exist.



(7) What is the output of this code?
input.setAttribute("type", "password");


A) Hides the input text
B) Displays it in uppercase
C) Makes the input read-only
D) Clears the input

Answer: A) Hides the input text

Explanation: Changing type to password hides user input as dots.



(8) How can you add a "title" attribute to a paragraph element?

A) p.addAttribute("title", "Info")
B) p.attribute = "title: Info"
C) p.setAttribute("title", "Info")
D) p.insert("title", "Info")

Answer: C) p.setAttribute("title", "Info")

Explanation: This is the correct syntax for adding a new attribute.



(9) Which method would you use to remove an attribute from an element?

A) removeAttr()
B) deleteAttribute()
C) element.removeAttribute()
D) element.clearAttr()

Answer: C) element.removeAttribute()

Explanation: This method is used to completely remove an attribute.



(10) Which attribute does an image element need to display the image properly?

A) alt
B) src
C) type
D) href

Answer: B) src

Explanation: The src attribute specifies the image source path.



(Chap # 66 - The DOM – Attribute names and values)

(1) Which method returns a list of all attribute names of an element?

A) getAttributeNames()
B) getAllAttributes()
C) listAttributes()
D) getAttrList()

Answer: A) getAttributeNames()

Explanation: getAttributeNames() returns an array of all attribute names on an element.



(2) What type of value does getAttributeNames() return?

A) String
B) Object
C) Array
D) Boolean

Answer: C) Array

Explanation: It returns an array of strings, where each string is an attribute name.



(3) What will element.attributes return?

A) An array of attribute values
B) A list of styles
C) A NamedNodeMap of attributes
D) A string of all attributes

Answer: C) A NamedNodeMap of attributes

Explanation: element.attributes returns a NamedNodeMap, not a regular array.



(4) How do you access the name of the first attribute using element.attributes?

A) element.attributes[0].name
B) element.attributes.name[0]
C) element.getAttributeNames()[0]
D) element.getAttribute()[0].name

Answer: A) element.attributes[0].name

Explanation: Each attribute is an object with name and value properties.



(5) What is the value of element.attributes[0].value?

A) The type of attribute
B) The attribute name
C) The value of the attribute
D) Always returns null

Answer: C) The value of the attribute

Explanation: .value retrieves the actual value assigned to the attribute.



(6) What kind of object is returned when using element.attributes?

A) Array
B) HTMLCollection
C) NodeList
D) NamedNodeMap

Answer: D) NamedNodeMap

Explanation: It's a special map-like structure with attribute nodes, not a true array.



(7) Which property gives the number of attributes on an element?

A) element.length
B) element.attributeCount
C) element.attributes.length
D) element.getAttributes().length

Answer: C) element.attributes.length

Explanation: attributes behaves like an array-like object with a length property.



(8) What does the following code return?
element.getAttributeNames().includes("class")


A) True if element has a class
B) False always
C) Returns attribute value
D) Throws error

Answer: A) True if element has a class

Explanation: It checks if "class" is one of the element's attributes.



(9) How can you loop through all attribute names of an element?

A) for (let attr of element.attributes)
B) for (let name of element.getAttributeNames())
C) Both A and B
D) None of these

Answer: C) Both A and B

Explanation: Both methods are valid for accessing all attribute names and values.



(10) In the NamedNodeMap, what does each item represent?

A) A DOM node
B) A property
C) An attribute node
D) A JavaScript object

Answer: C) An attribute node

Explanation: Each item in attributes is an attribute node with name and value.



(Chap # 67 - The DOM - Adding Nodes)

(1) Which method creates a new HTML element in JavaScript?

A) document.newElement()
B) document.createElement()
C) document.generateElement()
D) document.makeElement()

Answer: B) document.createElement()

Explanation: The correct method to create a new element is createElement().



(2) How do you create a text node in JavaScript?

A) document.newTextNode()
B) document.createTextNode()
C) document.generateText()
D) document.writeTextNode()

Answer: B) document.createTextNode()

Explanation: createTextNode() is used to generate a new text node.



(3) What method is used to add a node to a parent element?

A) appendChild()
B) addNode()
C) insertNode()
D) appendNode()

Answer: A) appendChild()

Explanation: appendChild() adds a node to the end of a parent element.



(4) Which of the following is the correct way to create and append a paragraph?

A) let p = document.create("p"); document.body.add(p);
B) let p = document.makeElement("p"); document.appendChild(p);
C) let p = document.createElement("p"); document.body.appendChild(p);
D) let p = new Element("p"); document.append(p);

Answer: C) let p = document.createElement("p"); document.body.appendChild(p);

Explanation: This is the standard method to create and append a new paragraph element.



(5) Which node must be added first before appending a text node to it?

A) body
B) div
C) A newly created element
D) The window object

Answer: C) A newly created element

Explanation: The text node must be appended to a valid element node created via createElement().



(6) What does appendChild() return?

A) True
B) The added child node
C) The parent node
D) Null

Answer: B) The added child node

Explanation: appendChild() returns the node that was added.



(7) What happens if you append a node that already exists elsewhere in the DOM?

A) It throws an error
B) It creates a copy
C) It removes the node from the old position and appends it to the new
D) Nothing happens

Answer: C) It removes the node from the old position and appends it to the new

Explanation: A node can exist in only one place at a time in the DOM.

(8) Can appendChild() be used to add multiple elements at once?

A) Yes, with comma-separated arguments
B) No, it only accepts one node at a time
C) Only in modern browsers
D) Only with special flag

Answer: B) No, it only accepts one node at a time

Explanation: To add multiple nodes, call appendChild() repeatedly or use append() method in modern JS.



(9) What is the output of the following code?
let div = document.createElement("div");
div.innerHTML = "Hello";
document.body.appendChild(div);


A) A div with text "Hello" will be added to the page
B) Nothing
C) It throws an error
D) The text will appear but not inside a div

Answer: A) A div with text "Hello" will be added to the page

Explanation: The div is created, assigned innerHTML, and appended to the body correctly.



(10) What method can append both elements and text together (in modern JS)?

A) appendChild()
B) addNode()
C) append()
D) write()

Answer: C) append()

Explanation: append() allows appending multiple nodes or strings at once.



(Chap # 68 - The DOM – Inserting nodes)

(1) What method allows you to insert a new node before an existing child node?

A) insertBefore()
B) addBefore()
C) insertNode()
D) appendBefore()

Answer: A) insertBefore()

Explanation: The insertBefore() method inserts a new node before an existing child node.



(2) What does insertBefore() require as its arguments?

A) Parent node and a reference node
B) The new node and the position before which to insert
C) The child node and parent node
D) New node and index

Answer: B) The new node and the position before which to insert

Explanation: The insertBefore() method requires the new node and an existing child node as the reference for insertion.



(3) Which method would you use to insert an element before another element in the DOM?

A) insertChild()
B) insertBefore()
C) addBefore()
D) insertElement()

Answer: B) insertBefore()

Explanation: insertBefore() is used to insert an element before another child in the DOM.



(4) What happens if you attempt to insert a node before a node that already exists in the DOM?

A) It throws an error
B) It overwrites the node
C) It appends the new node before the existing node
D) Nothing happens

Answer: C) It appends the new node before the existing node

Explanation: The new node is inserted before the specified reference node.



(5) What is required for the insertBefore() method to work?

A) The parent node must be specified
B) The node must already exist in the DOM
C) Both the reference node and the new node must be specified
D) The parent node needs to be added first

Answer: C) Both the reference node and the new node must be specified

Explanation: You need to specify both the new node and the reference node for insertBefore() to function correctly.



(6) Which of the following is an example of using insertBefore() to insert a new element?

A) parentNode.insertBefore(newElement, null);
B) document.insertBefore(parentNode, newElement);
C) newElement.insertBefore(parentNode, nextSibling);
D) parentNode.insertBefore(newElement, nextSibling);

Answer: D) parentNode.insertBefore(newElement, nextSibling);

Explanation: The correct syntax to insert a new element before an existing one is insertBefore(newElement, nextSibling).



(7) Which of the following is NOT a valid method for inserting nodes in the DOM?

A) insertBefore()
B) insertChild()
C) appendChild()
D) append()

Answer: B) insertChild()

Explanation: insertChild() is not a valid DOM method. The correct method is insertBefore().



(8) If no second argument is provided in insertBefore(), what happens?

A) It inserts at the end of the parent node
B) It throws an error
C) It inserts after the reference node
D) It overwrites the existing node

Answer: A) It inserts at the end of the parent node

Explanation: If no second argument is provided, the new node is appended to the parent element.



(9) What happens if the reference node is not part of the DOM when calling insertBefore()?

A) The insertion succeeds
B) The insertion fails and nothing happens
C) It throws a "Node not found" error
D) It creates a new reference node

Answer: C) It throws a "Node not found" error

Explanation: The reference node must be part of the DOM for insertBefore() to work properly.



(10) Can insertBefore() be used to insert a new node as the first child of a parent node?

A) Yes, by passing null as the second argument
B) Yes, by passing the reference node as the first child
C) No, it only inserts at the end
D) No, it cannot be used with null

Answer: B) Yes, by passing the reference node as the first child

Explanation: To insert a new node as the first child, pass the first existing child node as the reference node.



(Chap # 69 - Objects)

(1) What is the main feature of an object in JavaScript?

A) It can only store primitive values
B) It is a collection of key-value pairs
C) It only stores arrays
D) It can store only functions

Answer: B) It is a collection of key-value pairs

Explanation: In JavaScript, an object is a collection of key-value pairs, where the keys are unique identifiers for the values.



(2) How do you create a new object in JavaScript?

A) new Object();
B) {}
C) Object.create();
D) All of the above

Answer: D) All of the above

Explanation: You can create a new object in JavaScript using new Object();, {}, or Object.create();.



(3) What does the this keyword refer to in an object?

A) The object's name
B) The value of a property
C) The object itself
D) A global variable

Answer: C) The object itself

Explanation: The this keyword refers to the object in which it is used, allowing access to the object's properties and methods.



(4) How do you access the properties 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 the properties of an object using dot notation (object.property) or bracket notation (object["property"]).



(5) Which of the following is NOT a way to define an object in JavaScript?

A) Using object literal syntax
B) Using a constructor function
C) Using the Object.create() method
D) Using new Function()

Answer: D) Using new Function()

Explanation: The new Function() method is used for creating functions, not objects.



(6) What is the output of console.log(typeof obj) when obj is an object?

A) undefined
B) object
C) array
D) function

Answer: B) object

Explanation: In JavaScript, typeof returns object for objects, even though arrays and null are also considered objects.



(7) Which of the following statements correctly defines an object with two properties: name and age?

A) let person = {name: "John", age: 30};
B) let person = {name = "John", age = 30};
C) let person = new Object(name: "John", age: 30);
D) let person = (name: "John", age: 30);

Answer: A) let person = {name: "John", age: 30};

Explanation: The correct syntax for defining an object with properties in JavaScript is to use object literal syntax like in option A.



(8) What does Object.keys(obj) return?

A) The values of the object's properties
B) The names of the object's properties
C) The object's methods
D) An array of the object's property values and methods

Answer: B) The names of the object's properties

Explanation: The Object.keys() method returns an array of the names of an object's properties.



(9) Which method can be used to add new properties to an object?

A) Object.addProperty()
B) Object.define()
C) Object.assign()
D) You can add properties directly by assigning them to the object

Answer: D) You can add properties directly by assigning them to the object

Explanation: You can add new properties to an object directly by assigning values to the new properties, like obj.newProperty = value.



(10) What is the purpose of Object.freeze() in JavaScript?

A) To prevent an object from being modified
B) To delete an object
C) To make an object run faster
D) To convert an object to a string

Answer: A) To prevent an object from being modified

Explanation: The Object.freeze() method is used to freeze an object, preventing its properties from being added, deleted, or modified.



(1) Given the following code, what will be logged to the console?

let car = {make: "Toyota", model: "Corolla"};
car.make = "Honda";
console.log(car.make);


A) "Toyota"
B) "Honda"
C) "undefined"
D) Error

Answer: B) "Honda"

Explanation: The value of car.make is updated from "Toyota" to "Honda". When logged, it will display "Honda".



(2) What will the following code output?

let user = {name: "Alice", age: 25};
let person = Object.create(user);
console.log(person.name);


A) "Alice"
B) "undefined"
C) "null"
D) Error

Answer: A) "Alice"

Explanation: The person object is created with user as its prototype. The property name is inherited from the prototype.



(3) What is the output of this code?

let obj = {x: 1};
Object.freeze(obj);
obj.x = 2;
console.log(obj.x);


A) 1
B) 2
C) undefined
D) Error

Answer: A) 1

Explanation: After calling Object.freeze(), the object obj is immutable, meaning properties cannot be changed.



(4) How can you add a new property to an object person with the key city and value "New York"?

A) person.city = "New York";
B) Object.assign(person, {city: "New York"});
C) person["city"] = "New York";
D) All of the above

Answer: D) All of the above

Explanation: All options are correct ways to add a property to an object in JavaScript.



(5) Which code will remove the property age from an object person?

A) delete person.age;
B) person.remove(age);
C) person.age = undefined;
D) remove.person(age);

Answer: A) delete person.age;

Explanation: The delete operator is used to remove a property from an object in JavaScript.



(Chap # 70 - Objects : properties)

(1) What does Object.defineProperty() do in JavaScript?

A) It defines a property of an object with specific attributes
B) It adds a new method to an object
C) It removes a property from an object
D) It clones an object

Answer: A) It defines a property of an object with specific attributes

Explanation: Object.defineProperty() allows you to define a property and set its attributes (such as writable, enumerable, and configurable).



(2) What will be the output of the following code?

let car = {make: "Toyota", model: "Corolla"};
Object.defineProperty(car, "year", {value: 2021, writable: false});
car.year = 2022;
console.log(car.year);


A) 2022
B) 2021
C) undefined
D) Error

Answer: B) 2021

Explanation: The property year is defined with writable: false, so it cannot be modified. The value remains 2021.



(3) What will be the result of this code?

let person = {name: "Alice"};
Object.defineProperty(person, "name", {writable: false});
person.name = "Bob";
console.log(person.name);


A) Alice
B) Bob
C) undefined
D) Error

Answer: A) Alice

Explanation: Since the property name is defined with writable: false, it cannot be changed. The value remains "Alice".



(4) How do you access the properties of an object in JavaScript?

A) Using the dot notation or bracket notation
B) Using the get method
C) Using the set method
D) Using the apply method

Answer: A) Using the dot notation or bracket notation

Explanation: In JavaScript, object properties can be accessed using dot notation (e.g., person.name) or bracket notation (e.g., person["name"]).



(5) How do you define a read-only property using Object.defineProperty()?

A) Set writable: false
B) Set configurable: false
C) Set value as the property value
D) Set enumerable: false

Answer: A) Set writable: false

Explanation: To make a property read-only, you need to set the writable attribute to false using Object.defineProperty().



(6) What will be the output of the following code?

let obj = {};
Object.defineProperty(obj, "name", {value: "Alice", writable: false});
obj.name = "Bob";
console.log(obj.name);


A) Alice
B) Bob
C) undefined
D) Error

Answer: A) Alice

Explanation: Since name is defined as writable: false, the value cannot be changed and it remains "Alice".



(7) Which of the following will allow you to change a property's value without throwing an error?

A) Using Object.freeze()
B) Using Object.seal()
C) Using Object.defineProperty() with writable: true
D) Using Object.preventExtensions()

Answer: C) Using Object.defineProperty() with writable: true

Explanation: If a property is defined with writable: true using Object.defineProperty(), it can be changed.



(8) What is the output of this code?

let student = {name: "John"};
Object.defineProperty(student, "name", {value: "Doe", writable: false});
console.log(student.name);


A) John
B) Doe
C) undefined
D) Error

Answer: B) Doe

Explanation: The value of name is initially set to "Doe" using Object.defineProperty(), but the property is not writable so it cannot be changed afterward.



(9) How would you make a property enumerable in JavaScript?

A) Set enumerable: true in Object.defineProperty()
B) Set configurable: false
C) Use Object.freeze()
D) Use Object.preventExtensions()

Answer: A) Set enumerable: true in Object.defineProperty()

Explanation: In Object.defineProperty(), setting enumerable: true makes the property enumerable, meaning it will show up in for...in loops.



(10) What will happen if a property is defined with configurable: false?

A) You cannot delete the property
B) You cannot change the property's attributes
C) You cannot redefine the property
D) All of the above

Answer: D) All of the above

Explanation: When a property is defined with configurable: false, it cannot be deleted, its attributes cannot be modified, and it cannot be redefined.



βͺ ⏩

Prepared by "Ismail Shah"