Bird
Raised Fist0
Node.jsframework~20 mins

process.exit and exit codes in Node.js - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

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
Challenge - 5 Problems
🎖️
Node.js Exit Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the exit code of this Node.js script?
Consider this Node.js script. What exit code will the process return when it finishes?
Node.js
process.exit(5);
A1
B0
C5
Dundefined
Attempts:
2 left
💡 Hint
The number passed to process.exit() is the exit code.
Predict Output
intermediate
2:00remaining
What happens if you call process.exit() without arguments?
What exit code does Node.js use if process.exit() is called with no arguments?
Node.js
process.exit();
A1
BNaN
Cundefined
D0
Attempts:
2 left
💡 Hint
Default exit code when none is specified is zero.
component_behavior
advanced
2:00remaining
What will this script output before exiting?
Analyze this script. What will it print before the process exits?
Node.js
console.log('Start');
process.exit(2);
console.log('End');
AEnd
BStart
CStart\nEnd
DNo output
Attempts:
2 left
💡 Hint
process.exit stops the process immediately.
📝 Syntax
advanced
2:00remaining
Which option causes a syntax error?
Which of these process.exit calls will cause a syntax error in Node.js?
Aprocess.exit 1;
Bprocess.exit('1');
Cprocess.exit();
Dprocess.exit(0);
Attempts:
2 left
💡 Hint
Check the syntax for calling functions in JavaScript.
🔧 Debug
expert
2:00remaining
Why does this script exit?
This script calls process.exit(0) inside a setTimeout. Why does the process exit?
Node.js
setTimeout(() => {
  process.exit(0);
}, 1000);

setInterval(() => {
  console.log('Running');
}, 500);
ABecause setTimeout is delayed, but process.exit is called after 1 second, the process exits.
BBecause process.exit is asynchronous and delayed, the process stays alive.
CBecause setInterval prevents process.exit from stopping the process.
DBecause setInterval keeps the event loop alive, process.exit is never called.
Attempts:
2 left
💡 Hint
process.exit stops the process immediately when called.

Practice

(1/5)
1. What does process.exit(0) do in a Node.js program?
easy
A. Stops the program immediately and signals success
B. Stops the program immediately and signals an error
C. Pauses the program without exiting
D. Restarts the program automatically

Solution

  1. Step 1: Understand process.exit behavior

    process.exit immediately stops the Node.js program.
  2. Step 2: Interpret exit code 0

    An exit code of 0 means the program ended successfully without errors.
  3. Final Answer:

    Stops the program immediately and signals success -> Option A
  4. Quick Check:

    Exit code 0 = success [OK]
Hint: Exit code 0 means success, non-zero means error [OK]
Common Mistakes:
  • Thinking exit code 0 means error
  • Confusing pause with exit
  • Assuming process.exit restarts program
2. Which of the following is the correct syntax to exit a Node.js program with an error code 1?
easy
A. process.exit = 1;
B. exit.process(1);
C. process.exit(1);
D. process.exitCode = 1;

Solution

  1. Step 1: Identify correct method to exit

    The method to stop the program is process.exit() with a code inside parentheses.
  2. Step 2: Check syntax correctness

    process.exit(1); is the correct syntax to exit with code 1. Other options misuse method or assignment.
  3. Final Answer:

    process.exit(1); -> Option C
  4. Quick Check:

    Correct method call syntax = process.exit(1); [OK]
Hint: Use process.exit(code) with parentheses to exit [OK]
Common Mistakes:
  • Swapping method and object names
  • Assigning exit code instead of calling exit()
  • Missing parentheses in method call
3. What will be the exit code of this Node.js script?
console.log('Start');
process.exit(2);
console.log('End');
medium
A. Script runs forever
B. 1
C. 0
D. 2

Solution

  1. Step 1: Analyze the code flow

    The script prints 'Start', then calls process.exit(2), which stops the program immediately.
  2. Step 2: Determine exit code and output

    The exit code is 2. The line printing 'End' never runs.
  3. Final Answer:

    2 -> Option D
  4. Quick Check:

    process.exit(2) sets exit code 2 [OK]
Hint: Code after process.exit() does not run [OK]
Common Mistakes:
  • Assuming all console.logs run
  • Confusing exit code with default 0
  • Thinking exit code 2 means success
4. Identify the error in this Node.js code snippet:
process.exit = 1;
console.log('Exiting');
process.exit();
medium
A. process.exit is overwritten and no longer a function
B. process.exit() is called correctly with no error
C. Missing exit code argument in process.exit()
D. console.log should come after process.exit()

Solution

  1. Step 1: Check assignment to process.exit

    The code assigns 1 to process.exit, replacing the function with a number.
  2. Step 2: Understand effect on function call

    Calling process.exit() after overwriting causes an error because it's no longer a function.
  3. Final Answer:

    process.exit is overwritten and no longer a function -> Option A
  4. Quick Check:

    Overwriting process.exit breaks function call [OK]
Hint: Do not assign values to process.exit, it must stay a function [OK]
Common Mistakes:
  • Thinking missing argument causes error
  • Assuming console.log runs after exit
  • Ignoring that process.exit is a function
5. You want your Node.js script to exit with code 0 if a file exists, or code 3 if it does not. Which code snippet correctly implements this?
import fs from 'fs';
const file = 'data.txt';
if (fs.existsSync(file)) {
  process.exit(0);
} else {
  process.exit(3);
}
hard
A. Throws error because fs.existsSync is asynchronous
B. Correctly exits with 0 if file exists, 3 if not
C. Always exits with 0 regardless of file existence
D. Exits with 3 if file exists, 0 if not

Solution

  1. Step 1: Understand fs.existsSync usage

    fs.existsSync checks synchronously if the file exists, returning true or false.
  2. Step 2: Check exit codes in if-else

    If file exists, process.exit(0) runs; else process.exit(3) runs. This matches the requirement.
  3. Final Answer:

    Correctly exits with 0 if file exists, 3 if not -> Option B
  4. Quick Check:

    Sync check + exit codes used correctly [OK]
Hint: Use fs.existsSync for sync check, then process.exit(code) [OK]
Common Mistakes:
  • Using async fs.exists instead of sync
  • Swapping exit codes in if-else
  • Not calling process.exit with parentheses