Chapter 23 of this guide delves into advanced concepts in event handling. Strengthen your understanding of custom events, event propagation, debouncing, throttling, and memory leaks through these targeted multiple-choice questions.
Custom Events and Dispatching Events (8 MCQs)
Which method is used to create a custom event in JavaScript? a) createEvent() b) dispatchEvent() c) CustomEvent() d) generateEvent()
What is the main purpose of dispatching a custom event? a) To modify existing DOM elements b) To send specific event data and trigger listeners c) To debug application logic d) To handle asynchronous calls
What property in the CustomEvent constructor is used to pass custom data? a) data b) detail c) payload d) content
Which of the following is true about CustomEvent in JavaScript? a) It cannot bubble by default. b) It requires an external library to work. c) It is only supported in Node.js. d) It can propagate if specified.
What method is used to dispatch a custom event? a) sendEvent() b) dispatchCustom() c) triggerEvent() d) dispatchEvent()
When creating a CustomEvent, which value does bubbles default to if not specified? a) false b) true c) undefined d) null
How do you listen for a custom event in JavaScript? a) document.on() b) addEventListener() c) catchEvent() d) listenTo()
Can custom events carry custom data? a) Yes, using the detail property. b) No, custom data cannot be included. c) Yes, using the payload property. d) Yes, by modifying the eventType.
Event Propagation and Event Object (8 MCQs)
What are the three phases of event propagation in JavaScript? a) Capturing, Bubbling, Termination b) Capturing, Target, Bubbling c) Initializing, Bubbling, Stopping d) Sending, Receiving, Propagating
Which method prevents further propagation of an event? a) stopPropagation() b) preventDefault() c) cancelEvent() d) terminateEvent()
What does the event.target property represent? a) The element where the event listener is attached b) The element that triggered the event c) The type of event being dispatched d) The default action prevented by the event
What is the difference between event.target and event.currentTarget? a) target refers to the triggering element, and currentTarget refers to the element handling the event. b) target and currentTarget are interchangeable. c) target is specific to custom events, while currentTarget applies to all events. d) Both refer to the same element in the event loop.
How can you stop both the propagation and default behavior of an event? a) Use stopPropagation() and preventDefault() together. b) Use cancelEvent(). c) Set event.stopAll to true. d) It is not possible.
When is the capturing phase of event propagation executed? a) After the target phase b) Before the target phase c) After the bubbling phase d) Simultaneously with the bubbling phase
How can event delegation improve performance? a) By attaching event listeners only to the target elements b) By reducing the number of event listeners through parent delegation c) By preventing event propagation entirely d) By combining capturing and bubbling phases
Which property of the event object indicates if an event is cancelable? a) isCancel b) cancelable c) canCancel d) cancel
Debouncing and Throttling (8 MCQs)
What is the main purpose of debouncing? a) To delay function execution until after frequent events stop firing b) To execute a function at regular intervals c) To prioritize event listeners d) To prevent memory leaks in event handling
Which scenario is best suited for using throttling? a) Validating form inputs in real time b) Logging scroll events at fixed intervals c) Preventing double clicks on a button d) Handling keypress events
What parameter does a typical debounce function accept? a) Function, wait time b) Function, execution count c) Wait time, interval d) Event type, interval
How does throttling differ from debouncing? a) Throttling ensures execution at intervals, while debouncing delays execution until inactivity. b) Throttling prevents all events from triggering, debouncing executes immediately. c) Throttling and debouncing are identical. d) Debouncing is event-specific; throttling is not.
What is a common use case for debouncing? a) Handling click events b) Logging API calls c) Search input auto-suggestions d) Animating UI components
Which utility library provides functions for debouncing and throttling? a) Lodash b) jQuery c) Axios d) React
What happens if the debounce timer expires? a) The function is called immediately. b) The function is discarded. c) The timer resets. d) No action is taken.
How is throttling implemented in JavaScript? a) By wrapping the function with a setTimeout-based logic b) By using event.preventDefault() c) By modifying the event propagation phases d) By listening to events on a timer
Memory Leaks in Event Handling (6 MCQs)
What can lead to memory leaks in event handling? a) Improper removal of event listeners b) Using too many inline event handlers c) Using capturing phase exclusively d) Using addEventListener instead of onEvent
What does the removeEventListener() method do? a) Stops the event from bubbling b) Removes an event listener to prevent memory leaks c) Prevents event delegation d) Blocks future event listeners from being added
How can memory leaks in event listeners affect application performance? a) They reduce DOM element accessibility. b) They increase the memory consumption unnecessarily. c) They only impact server-side logic. d) They disable custom event dispatching.
Which of these is a preventive measure against memory leaks in event handling? a) Avoid using inline event handlers. b) Use named functions for event listeners. c) Use weak references for events. d) All of the above.
Why is it important to clean up event listeners in single-page applications (SPAs)? a) To prevent function overloading b) To avoid memory bloat and ensure smooth navigation c) To allow immediate re-rendering of the page d) To maintain the order of event propagation
What tool is commonly used to detect memory leaks in event handling? a) Chrome DevTools b) Visual Studio Code c) API Gateway d) React Profiler
Answer Key
Qno
Answer
1
c) CustomEvent()
2
b) To send specific event data and trigger listeners
3
b) detail
4
d) It can propagate if specified
5
d) dispatchEvent()
6
a) false
7
b) addEventListener()
8
a) Yes, using the detail property
9
b) Capturing, Target, Bubbling
10
a) stopPropagation()
11
b) The element that triggered the event
12
a) target refers to the triggering element, and currentTarget refers to the element handling the event
13
a) Use stopPropagation() and preventDefault() together
14
b) Before the target phase
15
b) By reducing the number of event listeners through parent delegation
16
b) cancelable
17
a) To delay function execution until after frequent events stop firing
18
b) Logging scroll events at fixed intervals
19
a) Function, wait time
20
a) Throttling ensures execution at intervals, while debouncing delays execution until inactivity
21
c) Search input auto-suggestions
22
a) Lodash
23
a) The function is called immediately
24
a) By wrapping the function with a setTimeout-based logic
25
a) Improper removal of event listeners
26
b) Removes an event listener to prevent memory leaks
27
b) They increase the memory consumption unnecessarily
28
d) All of the above
29
b) To avoid memory bloat and ensure smooth navigation