Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is an exit code in the context of a child process in Node.js?
An exit code is a number returned by a child process when it finishes running. It tells if the process ended successfully or if there was an error.
Click to reveal answer
beginner
What does an exit code of 0 usually mean for a child process?
An exit code of 0 means the child process finished successfully without errors.
Click to reveal answer
intermediate
How can you listen for a child process exit event in Node.js?
You can listen for the 'exit' event on the child process object to get the exit code and signal when the process ends.
Click to reveal answer
beginner
What does a non-zero exit code indicate?
A non-zero exit code means the child process ended with an error or was terminated unexpectedly.
Click to reveal answer
intermediate
What is the difference between the 'exit' and 'close' events on a child process?
The 'exit' event gives the exit code and signal when the process ends. The 'close' event fires after all stdio streams are closed, indicating the process fully finished.
Click to reveal answer
What does an exit code of 0 from a child process mean?
AProcess finished successfully
BProcess crashed
CProcess was killed by a signal
DProcess is still running
✗ Incorrect
An exit code of 0 means the process finished successfully without errors.
Which event do you listen to get the exit code of a child process in Node.js?
Aerror
Bstart
Cexit
Ddata
✗ Incorrect
The 'exit' event provides the exit code and signal when the child process ends.
What does a non-zero exit code usually indicate?
ASuccessful completion
BAn error or abnormal termination
CProcess is waiting
DProcess is paused
✗ Incorrect
Non-zero exit codes indicate errors or abnormal termination of the child process.
What is the difference between the 'exit' and 'close' events on a child process?
ANeither event provides exit code
B'close' fires when process ends; 'exit' fires after stdio streams close
CBoth fire at the same time
D'exit' fires when process ends; 'close' fires after stdio streams close
✗ Incorrect
'exit' event gives exit code and signal; 'close' event fires after all stdio streams close.
If a child process is terminated by a signal, what will the exit code be?
Anull or undefined
BNon-zero number
C0
DIt will be the signal number
✗ Incorrect
When terminated by a signal, the exit code is null and the signal name is provided instead.
Explain what child process exit codes are and how you can use them in Node.js.
Think about how a child process tells you if it finished well or had problems.
You got /4 concepts.
Describe the difference between the 'exit' and 'close' events on a Node.js child process and why both might be useful.
Consider what happens when a process ends versus when its output streams finish.
You got /4 concepts.
Practice
(1/5)
1. What does an exit code of 0 from a Node.js child process usually mean?
easy
A. The process finished successfully without errors.
B. The process was terminated by a signal.
C. The process encountered an error and crashed.
D. The process is still running.
Solution
Step 1: Understand exit codes in Node.js child processes
Exit codes indicate how a process ended. Code 0 means success.
Step 2: Interpret exit code 0
Exit code 0 means the process finished without errors or interruptions.
Final Answer:
The process finished successfully without errors. -> Option A
Quick Check:
Exit code 0 = success [OK]
Hint: Exit code 0 always means success in child processes [OK]
Common Mistakes:
Confusing exit code 0 with error codes
Thinking signal termination returns 0
Assuming non-zero codes mean success
2. Which of the following is the correct way to listen for a child process exit event in Node.js?
easy
A. child.listen('exit', (code) => { /* handle exit */ });
But the console never logs anything. What is the likely problem?
medium
A. The exit event callback is missing the signal parameter.
B. The child process is not exiting because the event loop is blocked.
C. The child process exits after 1 second, but the main program ends before that.
D. The spawn command syntax is incorrect.
Solution
Step 1: Analyze the child process behavior
The child process exits after 1 second due to setTimeout.
Step 2: Consider the main program lifecycle
If the main program ends before 1 second, it may exit before child finishes, so no log appears.
Final Answer:
The child process exits after 1 second, but the main program ends before that. -> Option C
Quick Check:
Main program must stay alive to see child exit [OK]
Hint: Keep main program alive to catch delayed child exit events [OK]
Common Mistakes:
Thinking missing signal parameter breaks event
Assuming spawn syntax is wrong without error
Ignoring asynchronous timing of child exit
5. You want to run a child process and handle both normal exit and signal termination. Which code snippet correctly logs the exit code or signal received?
hard
A. child.on('exit', (code, signal) => {
if (code !== null) console.log(`Exited with code ${code}`);
else console.log(`Terminated by signal ${signal}`);
});
B. child.on('exit', (code) => {
if (code === 0) console.log('Success');
else console.log('Error');
});
C. child.on('close', (signal) => {
console.log(`Closed with signal ${signal}`);
});
D. child.on('exit', () => {
console.log('Process ended');
});
Solution
Step 1: Understand exit event parameters
The 'exit' event provides two parameters: code (number or null) and signal (string or null).
Step 2: Handle both exit code and signal
If code is not null, process ended normally; if null, it was terminated by a signal.
Step 3: Check code snippet correctness
child.on('exit', (code, signal) => {
if (code !== null) console.log(`Exited with code ${code}`);
else console.log(`Terminated by signal ${signal}`);
}); correctly checks code and signal and logs accordingly.
Final Answer:
child.on('exit', (code, signal) => {
if (code !== null) console.log(`Exited with code ${code}`);
else console.log(`Terminated by signal ${signal}`);
}); -> Option A
Quick Check:
Check code !== null to distinguish exit vs signal [OK]
Hint: Check if exit code is null to detect signal termination [OK]
Common Mistakes:
Ignoring the signal parameter in exit event
Using 'close' event instead of 'exit' for exit codes