Node.js MCQs - Core Modules: fs, path, events, buffer, crypto and process (Set 5)

Node.js MCQs - Core Modules: fs, path, events, buffer, crypto and process (Set 5) - CodingHumans

Set 5 is about the modules that come with Node, the ones you use without installing anything: fs, path, events, buffer, crypto, child_process, http, process and os. Some questions ask for the exact output, and each explanation tells you why the module behaves like that.

How to practise: read each question and tap the option you think is correct. You will see right away whether you got it, along with a short explanation, and your score is tracked at the bottom of the screen. This set has 15 questions.

Node.js MCQ series: Set 1 | Set 2 | Set 3 | Interview Preparation Hub

Topics covered in this set

  • fs/promises and recursive directories
  • path.join, path.resolve and ESM __dirname
  • EventEmitter error handling and events.once
  • Buffer byte length and allocation
  • crypto: random values and safe comparison
  • child_process, http, process and os

Node.js MCQs with answers and explanations

Q1. What does await fs.readFile("a.txt") from node:fs/promises resolve to when no encoding is given?

a) An array of lines

b) A stream

c) A string

d) A Buffer

Q2. How do you create logs/2026/oct when none of the folders exist yet?

a) fs.writeFile("logs/2026/oct", "")

b) fs.mkdir("logs/2026/oct") always works

c) fs.createDir("logs/2026/oct")

d) await fs.mkdir("logs/2026/oct", { recursive: true })

Q3. What is the output?

path.join("/a/b", "../c", "d.txt")

a) "/a/b/../c/d.txt"

b) "/a/b/c/d.txt"

c) "/a/c/d.txt"

d) "/c/d.txt"

Q4. What is the output on Linux or macOS?

path.resolve("/a", "/b", "c")

a) "a/b/c"

b) "/a/b/c"

c) "/a/c"

d) "/b/c"

Q5. What does path.extname("archive.tar.gz") return?

a) "gz"

b) ".gz"

c) ".tar"

d) ".tar.gz"

Q6. In an ES module, how do you get the folder of the current file, since __dirname is not defined?

a) process.cwd()

b) require.main.path

c) import.meta.dirname, or path.dirname(fileURLToPath(import.meta.url)) in older versions

d) module.dirname

Q7. What happens when an EventEmitter emits "error" and no "error" listener is registered?

a) Nothing happens

b) The emitter retries

c) The error is logged and ignored

d) The error is thrown, and if not caught it crashes the process

Q8. What does await events.once(server, "listening") do?

a) Emits the event once

b) Returns a promise that resolves with the event arguments the first time the event fires

c) Removes all listeners

d) Registers a permanent listener

Q9. What is the output?

console.log(Buffer.from("héllo").length, "héllo".length);

a) 6 5

b) 10 5

c) 5 6

d) 5 5

Q10. What is the difference between Buffer.alloc(n) and Buffer.allocUnsafe(n)?

a) allocUnsafe cannot be written to

b) There is no difference

c) alloc is asynchronous

d) alloc fills the memory with zeros; allocUnsafe is faster but may contain old data until overwritten

Q11. Why compare a received HMAC signature with crypto.timingSafeEqual instead of ===?

a) timingSafeEqual takes the same time whatever the content, which prevents timing attacks that guess the signature byte by byte

b) timingSafeEqual is faster

c) It decrypts the signature

d) === does not work on strings

Q12. Which is appropriate for generating a password-reset token?

a) Date.now().toString()

b) Math.random().toString(36)

c) The user ID in Base64

d) crypto.randomBytes(32).toString("hex")

Q13. A command produces several gigabytes of output that you want to process as it arrives. Which function fits?

a) child_process.spawn

b) child_process.execSync

c) fs.readFile

d) child_process.exec

Q14. In a plain http.createServer((req, res) => { ... }) handler, how do you read a JSON request body?

a) Collect chunks from req (a readable stream) until "end", then JSON.parse the combined string

b) Read it from req.headers

c) req.body is already parsed

d) Call req.json()

Q15. After process.env.PORT = 3000, what is typeof process.env.PORT?

a) "undefined"

b) "number"

c) "string"

d) "object"

Quick revision notes

  • fs/promises gives a Buffer unless you pass an encoding. mkdir needs recursive: true for nested folders.
  • path.join cleans up the path. path.resolve builds an absolute path, working right to left.
  • An "error" event with no listener throws. events.once gives you a promise.
  • Buffer length is in bytes. alloc fills with zeros, allocUnsafe does not.
  • Use crypto.randomBytes and timingSafeEqual. spawn streams output, exec buffers it. Env values are always strings.

Frequently asked questions

Should I use fs callbacks, fs/promises or sync methods?

In servers, use fs/promises with async/await. Sync methods are fine in startup scripts and CLIs, but inside a request handler they block the event loop.

Why use the node: prefix in imports like node:fs?

It shows the module is built in, so nobody mixes it up with an npm package that has the same name.

What is the difference between process.exit() and setting process.exitCode?

process.exit() stops right away and can cut off writes that are still pending. Setting process.exitCode lets the process finish its work first, then exit with that code.

Comments

Answered 0/0Correct 0