Bird
0
0

Consider this Node.js code using clustering:

medium📝 Predict Output Q13 of 15
Node.js - Cluster Module
Consider this Node.js code using clustering:
const cluster = require('cluster');
if (cluster.isMaster) {
  cluster.fork();
  cluster.fork();
} else {
  console.log('Worker process running');
}

What will be the output when you run this code?
ANo output because cluster.fork() does not print anything.
BPrints 'Worker process running' twice, once for each worker.
CPrints 'Worker process running' once, from the master process.
DThrows an error because cluster.fork() is called twice.
Step-by-Step Solution
Solution:
  1. Step 1: Understand cluster.fork() behavior

    Each cluster.fork() creates a new worker process that runs the else block.
  2. Step 2: Count output lines

    Two forks mean two workers, each printing 'Worker process running' once.
  3. Final Answer:

    Prints 'Worker process running' twice, once for each worker. -> Option B
  4. Quick Check:

    Two forks = two worker outputs [OK]
Quick Trick: Each fork runs else block once, so output repeats per worker [OK]
Common Mistakes:
  • Thinking master prints the message
  • Assuming no output from workers
  • Believing multiple forks cause errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes