Bird
0
0

What output will this Node.js code produce?

medium📝 component behavior Q4 of 15
Node.js - Child Processes
What output will this Node.js code produce?
const { spawn } = require('child_process');
const child = spawn('node', ['-e', 'process.exit(10)']);
child.on('exit', (code) => console.log(`Code: ${code}`));
ACode: undefined
BCode: 10
CCode: null
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Understand process.exit()

    The child process explicitly exits with code 10.
  2. Step 2: Listen to 'exit' event

    The 'exit' event handler receives the exit code as the first argument.
  3. Final Answer:

    Code: 10 is logged, corresponding to Code: 10.
  4. Quick Check:

    Exit code passed to listener [OK]
Quick Trick: Exit event provides the exit code as first argument [OK]
Common Mistakes:
  • Assuming exit code is null or undefined
  • Not using the exit event properly
  • Expecting output without a listener

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes