JavaScript MCQs – DOM Manipulation and Events

21.) What does the DOMContentLoaded event signify?

A) The DOM is fully constructed.
B) The DOM is partially constructed.
C) The document and CSS are loaded but not images.
D) The document and all resources (images, CSS, etc.) are loaded.

Answer: Option A

Explanation: The DOMContentLoaded event is fired when the DOM is fully constructed but before external resources like images and stylesheets are loaded.

22.) Which of the following is NOT a valid DOM event?

A) click
B) change
C) mouseover
D) hover

Answer: Option D

Explanation: There is no hover event in the DOM. The hover effect is a CSS pseudo-class, not a JavaScript event.

23.) How do you detect which key was pressed during a keydown event?

A) event.key
B) event.which
C) event.code
D) All of the above

Answer: Option D

Explanation: All options can detect the key pressed, but event.key provides the character, and event.code specifies the physical key.

24.) What happens if you call stopPropagation() inside a capturing phase listener?

A) The event continues in the bubbling phase.
B) The event propagation stops completely.
C) Only the target element receives the event.
D) The event is ignored entirely.

Answer: Option B

Explanation: stopPropagation() halts the event’s propagation in both capturing and bubbling phases.

25.) What is the purpose of the focus event in JavaScript?

A) It triggers when an element gains focus.
B) It triggers when an element loses focus.
C) It triggers when a mouse hovers over an element.
D) It triggers when a user types inside an element.

Answer: Option A

Explanation: The focus event triggers when an element, such as an input field, gains focus.

26.) Which of the following methods removes all child nodes of a DOM element?

A) element.removeChildren()
B) element.innerHTML = “”
C) element.clear()
D) element.deleteChildren()

Answer: Option B

Explanation: Setting innerHTML to an empty string removes all child elements of a DOM node.

27.) How can you detect if an element has focus?

A) document.hasFocus()
B) element.isFocused()
C) document.activeElement === element
D) element.focused

Answer: Option C

Explanation: The document.activeElement property returns the currently focused element. Comparing it to the desired element verifies focus.

28.) What will the following code output?

const btn = document.querySelector("#myButton");  
btn.addEventListener("click", () => {  
    btn.disabled = true;  
    console.log("Button clicked!");  
});  
btn.click();
A) “Button clicked!” and the button becomes disabled.
B) “Button clicked!” and the button remains enabled.
C) Throws an error.
D) Nothing happens.

Answer: Option A

Explanation: The click() method simulates a click event programmatically. The event listener disables the button and logs the message.

29.) What will the following code output?

document.body.addEventListener("click", function(event) {
    console.log(event.currentTarget === document.body);
});
A) true
B) false
C) undefined
D) Throws an error

Answer: Option A

Explanation: The currentTarget refers to the element to which the event handler is attached. Here, it is document.body.

30.) What will the following code output?

let div = document.createElement("div");  
console.log(div.parentNode);
A) undefined
B) null
C) document.body
D) Throws an error.

Answer: Option B

Explanation: The div element is created but not yet attached to the DOM, so its parentNode is null.

Leave a Reply

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