Bird
0
0

Consider this code:

medium📝 component behavior Q5 of 15
Node.js - Child Processes
Consider this code:
const { spawn } = require('child_process');
const child = spawn('node', ['-e', 'setTimeout(() => process.exit(2), 100)']);
child.on('exit', (code) => console.log(code));

What will be printed after 100ms?
A0
Bnull
Cundefined
D2
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the child process code

    The child waits 100ms then calls process.exit(2), so it exits with code 2.
  2. Step 2: Confirm exit event logs the code

    The exit event logs the exit code, which will be 2 after 100ms.
  3. Final Answer:

    2 -> Option D
  4. Quick Check:

    Delayed exit code is logged correctly [OK]
Quick Trick: Exit code logs after process.exit call [OK]
Common Mistakes:
  • Expecting immediate exit code 0
  • Thinking exit code is null or undefined
  • Ignoring asynchronous delay

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes