0
0
Node.jsframework~20 mins

EventEmitter class in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
EventEmitter Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this EventEmitter code?
Consider this Node.js code using EventEmitter. What will it print when run?
Node.js
const EventEmitter = require('events');
const emitter = new EventEmitter();
emitter.on('greet', () => {
  console.log('Hello!');
});
emitter.emit('greet');
emitter.emit('greet');
AHello!
BHello!\nHello!
CNo output
DError: Event not found
Attempts:
2 left
💡 Hint
Think about how many times the 'greet' event is emitted and how many listeners are attached.
state_output
intermediate
2:00remaining
What is the value of count after this EventEmitter code runs?
Look at this code snippet. What is the final value of the variable count?
Node.js
const EventEmitter = require('events');
const emitter = new EventEmitter();
let count = 0;
emitter.on('increment', () => {
  count += 1;
});
emitter.emit('increment');
emitter.emit('increment');
emitter.emit('increment');
A0
B1
C3
DError: count is not defined
Attempts:
2 left
💡 Hint
Each 'increment' event increases count by 1.
📝 Syntax
advanced
2:00remaining
Which option causes a syntax error in EventEmitter usage?
Which of these code snippets will cause a syntax error when using EventEmitter?
Aemitter.on 'event', () => console.log('Hi')
Bemitter.emit('event')
Cemitter.on('event', () => console.log('Hi'))
Demitter.once('event', () => console.log('Hi'))
Attempts:
2 left
💡 Hint
Check the syntax for calling methods in JavaScript.
🔧 Debug
advanced
2:00remaining
Why does this EventEmitter code not print anything?
This code does not print anything. What is the reason?
Node.js
const EventEmitter = require('events');
const emitter = new EventEmitter();
emitter.emit('start');
emitter.on('start', () => {
  console.log('Started');
});
AThe listener is added after the event is emitted, so it misses the event.
BThe event name 'start' is invalid and ignored.
Cemit() does not trigger listeners in EventEmitter.
Dconsole.log is disabled by default in EventEmitter.
Attempts:
2 left
💡 Hint
Think about the order of adding listeners and emitting events.
🧠 Conceptual
expert
2:00remaining
What happens if an EventEmitter listener throws an error and there is no 'error' listener?
In Node.js EventEmitter, if a listener throws an error and no 'error' event listener is registered, what happens?
AThe EventEmitter automatically retries the listener.
BThe error is silently ignored and the program continues.
CThe error is logged to console but the program continues.
DThe process crashes with an uncaught exception.
Attempts:
2 left
💡 Hint
Consider Node.js default behavior for unhandled errors in EventEmitter.