Bird
0
0

Given this code snippet:

medium📝 Debug Q14 of 15
Node.js - Cluster Module
Given this code snippet:
const cluster = require('cluster');
if (cluster.isMaster) {
  cluster.fork();
  cluster.fork();
} else {
  console.log('Worker started');
}

Why might the workers never start properly?
ABecause the master process forgot to require('http')
BBecause the workers have no code to keep them alive
CBecause cluster.fork() is called inside the else block
DBecause cluster.isMaster is always false
Step-by-Step Solution
Solution:
  1. Step 1: Analyze worker code behavior

    The workers only log 'Worker started' and then exit immediately because no server or event loop keeps them alive.
  2. Step 2: Understand cluster.fork usage

    cluster.fork() is correctly called in master block, so workers start but exit quickly.
  3. Final Answer:

    Because the workers have no code to keep them alive -> Option B
  4. Quick Check:

    Workers exit if no server or event loop runs [OK]
Quick Trick: Workers need active code (like server) to stay alive [OK]
Common Mistakes:
  • Thinking missing http require stops workers
  • Confusing cluster.fork placement
  • Assuming cluster.isMaster is always false

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes