JavaScript MCQs – DOM Manipulation and Events

JavaScript MCQs – DOM Manipulation and Events is an important topic for interview preparation. It focuses on working with DOM elements, handling events, and understanding concepts like event propagation, delegation, and methods such as addEventListener and querySelector. Practicing MCQs on these topics helps you build a strong foundation and prepares you for coding interviews.

1.) What does DOM stand for in JavaScript?

A) Data Object Model
B) Document Object Model
C) Dynamic Object Model
D) Document Oriented Model

Answer: Option B

Explanation: DOM stands for Document Object Model. It represents the structure of an HTML document as a tree of objects.

2.) Which method is used to select an element by its ID in JavaScript?

A) getElementByClass
B) getElementsByTag
C) getElementById
D) querySelector

Answer: Option C

Explanation: document.getElementById selects an HTML element based on its unique ID.

3.) What does querySelector() do?

A) Selects all elements of a given type.
B) Selects the first matching element.
C) Selects elements by class only.
D) Selects elements by ID only.

Answer: Option B

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

4.) How can you add a new child element to an existing DOM element?

A) appendChild()
B) addChild()
C) createChild()
D) insertChild()

Answer: Option A

Explanation: The appendChild() method adds a new child node to an existing element.

5.) Which property is used to access or change the content of an HTML element?

A) innerContent
B) innerText
C) innerHTML
D) outerHTML

Answer: Option C

Explanation: The innerHTML property is used to get or set the HTML content of an element.

6.) What will the following code do?

document.getElementById("myDiv").style.color = "red";
A) Do nothing.
B) Throw an error.
C) Change the background color of myDiv to red.
D) Change the text color of myDiv to red.

Answer: Option D

Explanation: The style.color property changes the text color of the selected element.

7.) How do you attach a click event to a button in JavaScript?

A) button.onClick = function() { … }
B) button.addEventListener(“click”, function() { … })
C) button.attach(“click”, function() { … })
D) Both A and B

Answer: Option D

Explanation: Both onClick and addEventListener can be used to attach click events, but addEventListener is preferred for modern development.

8.) What does the preventDefault() method do in an event handler?

A) Stops the execution of the event handler.
B) Stops the event from propagating.
C) Prevents the default action of the event.
D) Removes the event listener.

Answer: Option C

Explanation: The preventDefault() method prevents the browser’s default action for an event (e.g., navigating on a link click).

9.) How can you remove an existing child element in the DOM?

A) removeChild()
B) deleteChild()
C) removeElement()
D) clearChild()

Answer: Option A

Explanation: The removeChild() method removes a specified child node from a parent node.

10.) What is the difference between stopPropagation() and preventDefault()?

A) Both stop the event from propagating.
B) Both prevent the default behavior.
C) stopPropagation() stops event bubbling, while preventDefault() stops the default action.
D) There is no difference.

Answer: Option C

Explanation: stopPropagation() stops event bubbling or capturing, while preventDefault() stops the browser’s default behavior for an event.

Leave a Reply

Your email address will not be published. Required fields are marked *