Bird
0
0

Consider this code snippet:

medium📝 component behavior Q13 of 15
Node.js - Child Processes
Consider this code snippet:
const { spawn } = require('child_process');
const child = spawn('node', ['-e', "process.exit(1)"]);
child.on('exit', (code) => {
  console.log('Exit code:', code);
});
child.on('error', (err) => {
  console.error('Error:', err);
});

What will be printed when this runs?
AExit code: 0
BError: Error message
CExit code: 1
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Understand the child process command

    The child runs a Node.js command that immediately exits with code 1 using process.exit(1).
  2. Step 2: Check which event triggers

    The process exits normally with code 1, so the 'exit' event fires with code 1; no 'error' event occurs.
  3. Final Answer:

    Exit code: 1 -> Option C
  4. Quick Check:

    process.exit(1) triggers 'exit' with code 1 [OK]
Quick Trick: process.exit(n) triggers 'exit' with code n, no 'error' [OK]
Common Mistakes:
  • Thinking exit code 1 triggers 'error' event
  • Expecting 'Exit code: 0' by default
  • Ignoring the 'exit' event output

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes