Environment & Configuration Questions with Answers

14 practice questions on Environment & Configuration (Node.js & Backend APIs) with answers and step-by-step solutions. Try 10 samples below, then practise all 14 free in the AptiNest app.

Sample questions

Question 1Easy

What does const port = process.env.PORT || 3000; do?

  1. Always uses 3000
  2. Uses PORT if set and non-empty, else 3000
  3. Sets the PORT variable to 3000
  4. Throws if PORT is not defined

Answer: B. Uses PORT if set and non-empty, else 3000

Explanation

|| falls back when the left side is falsy; an unset variable is undefined and an empty string is falsy too.

Step-by-step solution

  1. Const port = process.env.PORT || 3000; checks if PORT is set and not empty.
  2. If PORT is defined and has a value, it uses that value for port.
  3. If PORT is undefined or an empty string, it defaults to 3000.
  4. This ensures a fallback value when the environment variable is missing.
  5. The correct answer is Uses PORT if set and non-empty, else 3000.

Why the other options are wrong

  • A. Always using 3000 would ignore PORT, but the || operator uses PORT when it is set.
  • C. The assignment does not write PORT back to the environment; it only creates a local const.
  • D. A missing PORT gives undefined, which is falsy, so no error is thrown; 3000 is used.

Tip: Use || to provide fallback values for optional environment variables.

Question 2Easy

Should a project's .env file containing API keys be committed to Git?

  1. Yes, so that every developer on the team shares exactly the same keys
  2. Yes, if the repository is private
  3. No; list it in .gitignore and share an example file
  4. Only if the keys are base64 encoded

Answer: C. No; list it in .gitignore and share an example file

Explanation

Secrets in Git history are hard to remove and leak easily. Commit a .env.example without real values instead.

Step-by-step solution

  1. API keys should never be committed to Git as they are sensitive data.
  2. Committing them risks exposing secrets to the public if the repo is public.
  3. Instead, list .env in .gitignore to prevent accidental commits.
  4. Share a .env.example file with placeholder values for team reference.
  5. Final answer: No; list it in .gitignore and share an example file.

Why the other options are wrong

  • A. Sharing the same keys is not a reason to commit them; secrets in Git stay in history.
  • B. A private repository can still be cloned, leaked or forked, so keys should stay out of Git.
  • D. Base64 is an encoding, not encryption, so encoded keys are still readable and must stay out of Git.

Tip: Always use .gitignore for secrets and share example files instead of real ones.

Question 3Easy

For the command node app.js --port 8080, what is process.argv[2]?

  1. '8080'
  2. 'app.js'
  3. '--port'
  4. 'node'

Answer: C. '--port'

Explanation

argv[0] is the node binary path and argv[1] the script path, so user arguments start at index 2.

Step-by-step solution

  1. process.argv[0] is the node binary and process.argv[1] is the script path.
  2. Arguments that follow the script start at index 2.
  3. So process.argv[2] is the first user argument, '--port', which is option C.

Why the other options are wrong

  • A. '8080' is at index 3, not index 2.
  • B. 'app.js' is the script path, at index 1.
  • D. 'node' is the program path, at index 0.

Tip: argv[0] is node and argv[1] is the script; user arguments start at index 2.

Question 4Easy

Which Node.js command-line flag loads environment variables from a file without any npm package?

  1. --require-env
  2. --dotenv
  3. --config
  4. --env-file

Answer: D. --env-file

Explanation

node --env-file=.env app.js reads KEY=value lines into process.env before the app starts.

Step-by-step solution

  1. Node 20.6 and later can read a .env file with a built-in flag.
  2. The flag is --env-file=path, which needs no npm package.
  3. Of the options, only --env-file is a real flag, so the answer is option D.

Why the other options are wrong

  • A. --require-env is not a Node flag.
  • B. --dotenv is not a built-in Node flag; it is a common npm package name.
  • C. --config is not a built-in Node flag for loading environment files.

Tip: Use node --env-file=.env app.js for simple setups; packages such as dotenv are only needed for older Node versions.

Question 5Medium

Why validate required environment variables when the app starts?

  1. It fails fast with a clear error, not later mid-request
  2. Validation makes reading variables faster
  3. Node refuses to start without validation
  4. It encrypts the values in memory

Answer: A. It fails fast with a clear error, not later mid-request

Explanation

A missing DATABASE_URL found at boot is easy to fix; found hours later inside a request it is much harder to diagnose.

Step-by-step solution

  1. Missing variables early show errors quickly, helping fix issues fast.
  2. Waiting until mid-request hides errors, making debugging harder.
  3. Validation at start ensures all required values are present and correct.
  4. This prevents unexpected failures during critical operations.
  5. Fast error detection saves time and reduces troubleshooting effort.

Why the other options are wrong

  • B. Validation does not make reading variables faster; its job is to catch missing settings early.
  • C. Node starts without validation; the app only fails if the code checks and throws.
  • D. Validation does not encrypt values; it only checks that they are present and well formed.

Tip: Validate variables at startup to avoid hidden bugs later.

Question 6Medium

What does this print when MISSING_VAR_XYZ is not set?

console.log(Number(process.env.MISSING_VAR_XYZ));
  1. 0
  2. NaN
  3. undefined
  4. null

Answer: B. NaN

Explanation

Number(undefined) is NaN; Number("") would be 0. Validate numeric config at startup to catch this.

Step-by-step solution

  1. Missing env var is undefined.
  2. Number(undefined) is NaN.

Why the other options are wrong

  • A. Number('') would be 0.
  • C. Number converts to a number.
  • D. Number(null) would be 0.

Tip: Validate config.

Question 7Medium

Why build one artifact and run it in dev, staging and production with different environment variables?

  1. Builds are only allowed once per project
  2. Environment variables make the code smaller
  3. What you tested is exactly what you deploy
  4. Production servers cannot run build tools

Answer: C. What you tested is exactly what you deploy

Explanation

Rebuilding per environment risks differences between the tested and deployed code. Config by environment keeps the artifact identical.

Step-by-step solution

  1. Building separately for each environment means the tested build may differ from the deployed one.
  2. Building once and changing only environment variables means the same code and bundle runs everywhere.
  3. So the reason is that what you tested is exactly what you deploy, which is option C.

Why the other options are wrong

  • A. Builds are not limited to one per project.
  • B. Environment variables configure behaviour; they do not make code smaller.
  • D. Production servers can run builds; that is not the reason for one artifact.

Tip: Build once, store the artifact, and configure each environment through its variables.

Question 8Medium

A Node script sets process.env.MODE = 'test'. After the script exits, what is MODE in the shell that launched it?

  1. "test", because a child process shares its environment with the shell
  2. "test" until the terminal restarts
  3. Deleted from the shell
  4. Unchanged; a child cannot alter its parent's environment

Answer: D. Unchanged; a child cannot alter its parent's environment

Explanation

Each process gets a copy of its parent's environment. Changes affect the process and its own children only.

Step-by-step solution

  1. Each process gets a copy of the environment.
  2. Changes in the child do not reach the parent.

Why the other options are wrong

  • A. The environment is copied.
  • B. The shell is not changed.
  • C. It was never in the shell.

Tip: Copy, not share.

Question 9Hard

What does this print?

process.env.DEBUG = false;
if (process.env.DEBUG) console.log('on');
else console.log('off');
  1. off
  2. false
  3. undefined
  4. on

Answer: D. on

Explanation

The env value becomes the string "false", and any non-empty string is truthy. Compare against "true" explicitly instead.

Step-by-step solution

  1. Assigning to process.env converts the value to a string, so DEBUG becomes the string 'false'.
  2. The non-empty string 'false' is truthy in JavaScript.
  3. The if branch runs and prints 'on', so the answer is option D.

Why the other options are wrong

  • A. The if test is true because the string 'false' is not empty.
  • B. The text 'false' is never printed; only 'on' or 'off' are printed.
  • C. The variable is set, so it is not undefined.

Tip: Every value in process.env is a string; compare with === 'true' or 'false' rather than relying on truthiness.

Question 10Hard

What does this print?

process.env.RETRIES = '0';
const a = Number(process.env.RETRIES) || 3;
const b = Number(process.env.RETRIES) ?? 3;
console.log(a, b);
  1. 0 0
  2. 3 0
  3. 3 3
  4. 0 3

Answer: B. 3 0

Explanation

|| replaces any falsy value, including a valid 0. ?? replaces only null and undefined, so it keeps 0.

Step-by-step solution

  1. RETRIES is the string '0', and Number('0') gives the number 0.
  2. A: 0 || 3 is 3, because || skips falsy values such as 0.
  3. B: 0 ?? 3 is 0, because ?? only skips null and undefined. The output is '3 0', which is option B.

Why the other options are wrong

  • A. A is 3, because || treats 0 as falsy.
  • C. B is 0, because ?? keeps 0 as a valid value.
  • D. A is 3, not 0, because || replaces the falsy value 0.

Tip: Use ?? for defaults when 0 or an empty string is a real value; || replaces every falsy value.

Practise all 14 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Node.js & Backend APIs topics · All topics