Bird
0
0

What will the following code output?

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

// child.js content:
process.on('message', msg => {
  process.send(msg + ' World');
});
AParent got: World
BParent got: Hello World
CNo output, error occurs
DParent got: Hello
Step-by-Step Solution
Solution:
  1. Step 1: Trace the message flow and parent's message handler

    The parent sends 'Hello' to child; child appends ' World' and sends back. Parent logs 'Parent got:' plus the message received from child.
  2. Final Answer:

    Parent got: Hello World -> Option B
  3. Quick Check:

    Child appends ' World' and parent logs it [OK]
Quick Trick: Child appends ' World' before sending back [OK]
Common Mistakes:
  • Expecting parent to log original 'Hello' only
  • Thinking child does not send a message back
  • Confusing message event direction

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes