Bird
0
0

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

medium📝 component behavior Q13 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('Parent received:', msg);
});
child.send('Hello Child');

// child.js content:
// process.on('message', (msg) => {
//   process.send(msg + ' from Child');
// });
ANo output because child.js is missing
BParent received: Hello Child
CError: child.send is not a function
DParent received: Hello Child from Child
Step-by-Step Solution
Solution:
  1. Step 1: Understand message passing between parent and child

    The parent sends 'Hello Child' to the child process. The child listens for messages and replies by appending ' from Child'.
  2. Step 2: Trace the output

    The parent listens for messages from the child and logs them. So it logs: 'Parent received: Hello Child from Child'.
  3. Final Answer:

    Parent received: Hello Child from Child -> Option D
  4. Quick Check:

    Message sent and replied correctly = D [OK]
Quick Trick: Child replies with modified message; parent logs it [OK]
Common Mistakes:
  • Assuming child.send is undefined
  • Ignoring message event listeners
  • Thinking output is only 'Hello Child'

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes