Bird
0
0

What will be the output of this Node.js code snippet?

medium📝 component behavior Q4 of 15
Node.js - Child Processes
What will be the output of this Node.js code snippet?
const { fork } = require('child_process');
const child = fork('child.js');
child.on('message', msg => console.log(msg));
child.send('Hello');

Assuming 'child.js' sends back the message it receives.
ANo output
Bundefined
CError: child.js not found
DHello
Step-by-Step Solution
Solution:
  1. Step 1: Understand fork and message passing

    Fork creates a child process running 'child.js'. The parent sends 'Hello' and listens for messages.
  2. Step 2: Behavior of child.js

    If 'child.js' sends back the received message, the parent will log 'Hello'.
  3. Final Answer:

    Hello -> Option D
  4. Quick Check:

    Message passing works correctly = D [OK]
Quick Trick: fork allows two-way messaging between parent and child [OK]
Common Mistakes:
  • Assuming no output without explicit console.log in child.js
  • Confusing fork with spawn behavior
  • Expecting error if child.js exists

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes