...

/

Quiz: Event-Driven Programming

Quiz: Event-Driven Programming

Test your understanding of event-driven programming in Node.js with this comprehensive quiz.

We'll cover the following...
Technical Quiz
1.

In the countdown timer example given below, we want to allow multiple countdowns to run simultaneously without interfering with each other. Which approach achieves this?

const EventEmitter = require('events');

// Create an instance of EventEmitter
const countdown = new EventEmitter();

// Add listeners
countdown.on('tick', (time) => {
    console.log(`Time remaining: ${time} seconds`);
});

countdown.on('end', () => {
    console.log('Countdown complete!');
});

// Function to start the countdown
function startCountdown(seconds) {
    let remaining = seconds;

    const interval = setInterval(() => {
        if (remaining > 0) {
            countdown.emit('tick', remaining);
            remaining--;
        } else {
            clearInterval(interval);
            countdown.emit('end');
        }
    }, 1000);
}

// Start a 5-second countdown
startCountdown(5);
A.

Use a single EventEmitter instance and start multiple countdowns.

B.

Modify the existing EventEmitter to handle multiple timers internally.

C.

Create a new EventEmitter instance for each countdown timer.

D.

Use setTimeout instead of setInterval.


1 / 5
...