Skip to content
QuiznExam

Event Emitter in Node.js

Test your knowledge of the Node.js EventEmitter API, including creating emitters, registering listeners, emitting events, removing listeners, and handling event-driven programming.

15
Questions
12 min
Time limit
Medium
Difficulty

No sign-up needed · Last updated:

Save your score

At least 8 characters.

Forgot password?

Related quizzes

All 15 questions and answers Spoilers: this shows the questions and answers. Try the quiz first! Open
  1. 1. What is EventEmitter in Node.js?

    • A. A mechanism for implementing event-driven communication
    • B. A database connection manager
    • C. A file compression utility
    • D. A package manager
    Show answer

    A. A mechanism for implementing event-driven communication

    EventEmitter provides an event-driven mechanism where objects can emit named events and listeners can respond to them.

  2. 2. Which built-in Node.js module provides the EventEmitter class?

    • A. events
    • B. event
    • C. emitter
    • D. stream-events
    Show answer

    A. events

    The built-in node:events module provides the EventEmitter class.

  3. 3. Which method is commonly used to create a listener for an event?

    • A. on()
    • B. listen()
    • C. subscribeEvent()
    • D. watch()
    Show answer

    A. on()

    The on() method registers a listener that is called whenever the specified event is emitted.

  4. 4. Which method is used to trigger an event on an EventEmitter?

    • A. emit()
    • B. trigger()
    • C. fire()
    • D. send()
    Show answer

    A. emit()

    The emit() method synchronously calls the listeners registered for the specified event.

  5. 5. Which code correctly imports EventEmitter using CommonJS?

    • A. const { EventEmitter } = require('node:events');
    • B. const EventEmitter = import('events');
    • C. require EventEmitter from 'events';
    • D. import EventEmitter = requireEvent('events');
    Show answer

    A. const { EventEmitter } = require('node:events');

    CommonJS can import EventEmitter by destructuring it from the built-in node:events module.

  6. 6. How can you create an instance of EventEmitter?

    • A. const emitter = new EventEmitter();
    • B. const emitter = EventEmitter.create();
    • C. const emitter = EventEmitter.new();
    • D. const emitter = create EventEmitter();
    Show answer

    A. const emitter = new EventEmitter();

    EventEmitter is a class, so an instance can be created using the new keyword.

  7. 7. Which method registers a listener that is automatically removed after being called once?

    • A. once()
    • B. single()
    • C. oneTime()
    • D. listenOnceOnly()
    Show answer

    A. once()

    The once() method registers a listener that is invoked at most once for the specified event.

  8. 8. What happens when an event is emitted with arguments?

    • A. The arguments are passed to the event listeners
    • B. The arguments are automatically discarded
    • C. The event is converted into a Promise
    • D. The listeners are removed
    Show answer

    A. The arguments are passed to the event listeners

    Arguments supplied to emit() after the event name are passed to the registered listeners in the same order.

  9. 9. Which method can be used to remove a specific listener from an EventEmitter?

    • A. off()
    • B. removeListener()
    • C. Both A and B
    • D. deleteListenerOnly()
    Show answer

    C. Both A and B

    Node.js provides both off() and removeListener() for removing a previously registered listener.

  10. 10. What does the event name passed to emit() identify?

    • A. The event whose registered listeners should be called
    • B. The Node.js module to load
    • C. The process ID
    • D. The name of the JavaScript file
    Show answer

    A. The event whose registered listeners should be called

    The event name identifies which listeners should be invoked when emit() is called.

  11. 11. What is special about the 'error' event in EventEmitter?

    • A. If emitted without an error listener, Node.js throws the error and the process can terminate
    • B. It is ignored automatically
    • C. It can only be emitted once
    • D. It is automatically converted into a warning
    Show answer

    A. If emitted without an error listener, Node.js throws the error and the process can terminate

    The error event has special behavior. If no error listener is registered when an error event is emitted, Node.js treats it as an uncaught exception.

  12. 12. Which method returns the number of listeners registered for a specific event?

    • A. listenerCount()
    • B. countListenersOnly()
    • C. getListenersNumber()
    • D. eventCount()
    Show answer

    A. listenerCount()

    The listenerCount() method returns the number of listeners currently registered for a specified event.

  13. 13. What is the purpose of removeAllListeners()?

    • A. To remove listeners from an EventEmitter
    • B. To remove all Node.js modules
    • C. To stop the Node.js process
    • D. To delete all emitted events
    Show answer

    A. To remove listeners from an EventEmitter

    removeAllListeners() removes all listeners, or all listeners for a specified event when an event name is provided.

  14. 14. How are EventEmitter listeners normally executed when an event is emitted?

    • A. Synchronously in the order they were registered
    • B. Always in random order
    • C. Only after the application exits
    • D. Only in separate processes
    Show answer

    A. Synchronously in the order they were registered

    EventEmitter normally invokes listeners synchronously in the order they were registered.

  15. 15. Which statement about EventEmitter in Node.js is correct?

    • A. It allows objects to emit named events and other code to listen for those events
    • B. It is used only for handling HTTP requests
    • C. It can emit only predefined events
    • D. It replaces all asynchronous programming in Node.js
    Show answer

    A. It allows objects to emit named events and other code to listen for those events

    EventEmitter is a general-purpose event-driven API used throughout Node.js for communicating through named events.