Bird
0
0

Given this code snippet, what will be logged when a worker exits unexpectedly?

medium📝 Predict Output Q4 of 15
Node.js - Cluster Module
Given this code snippet, what will be logged when a worker exits unexpectedly?
const cluster = require('cluster');
if (cluster.isMaster) {
  cluster.fork();
  cluster.on('exit', (worker, code, signal) => {
    console.log(`Worker ${worker.process.pid} died`);
  });
}
ALogs 'Worker [pid] died' when a worker exits
BNothing, because the exit event is not handled
CThrows an error because cluster.on is invalid
DLogs 'Master process exited' when a worker dies
Step-by-Step Solution
Solution:
  1. Step 1: Understand cluster event handling

    The exit event on cluster triggers when a worker dies, and the callback logs the worker's pid.
  2. Step 2: Analyze the code behavior

    Since the event is handled properly, it logs the message with the worker's pid on exit.
  3. Final Answer:

    Logs 'Worker [pid] died' when a worker exits -> Option A
  4. Quick Check:

    cluster.on('exit') logs worker death = A [OK]
Quick Trick: cluster.on('exit') handles worker death events [OK]
Common Mistakes:
  • Assuming exit event is not handled
  • Thinking cluster.on is invalid
  • Confusing worker exit with master exit

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes