Bird
0
0

Given the code below, what will be logged when a worker crashes?

medium📝 component behavior Q13 of 15
Node.js - Cluster Module
Given the code below, what will be logged when a worker crashes?
const cluster = require('cluster');
if (cluster.isMaster) {
  const worker = cluster.fork();
  worker.on('exit', (code, signal) => {
    console.log(`Worker exited with code ${code} and signal ${signal}`);
  });
} else {
  process.exit(1); // Simulate crash
}
AWorker exited with code null and signal SIGTERM
BWorker exited with code 0 and signal null
CWorker exited with code 1 and signal null
DNo output because exit event is not triggered
Step-by-Step Solution
Solution:
  1. Step 1: Understand process.exit(1) effect

    Calling process.exit(1) ends the worker with exit code 1, indicating an error.
  2. Step 2: Check exit event parameters

    The exit event callback receives the exit code and signal; here signal is null because no signal caused the exit.
  3. Final Answer:

    Worker exited with code 1 and signal null -> Option C
  4. Quick Check:

    Exit code 1 means crash, signal null if no signal [OK]
Quick Trick: Exit code 1 means crash, signal null if no signal sent [OK]
Common Mistakes:
  • Assuming exit code 0 means crash
  • Confusing signal with exit code
  • Thinking exit event won't fire on crash

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes