Node.js Core Modules & Event Loop 3 — Questions and Answers
Question 1: Which Node.js core module allows you to create child processes?
- process
- child_process (Correct answer)
- cluster
- worker_threads
Correct answer: child_process
The `child_process` module provides `spawn`, `exec`, `execFile`, and `fork` to create and communicate with child processes.
Question 2: What is the difference between `Buffer.alloc()` and `Buffer.allocUnsafe()`?
- alloc() is faster; allocUnsafe() is zero-filled
- alloc() zero-fills the memory; allocUnsafe() does not and may contain old data (Correct answer)
- allocUnsafe() throws on large sizes; alloc() does not
- They are identical in Node.js v16+
Correct answer: alloc() zero-fills the memory; allocUnsafe() does not and may contain old data
`Buffer.alloc()` initializes memory to zero for security, while `Buffer.allocUnsafe()` skips initialization for speed but may expose old memory contents.
Question 3: When both `setImmediate()` and `setTimeout(fn, 0)` are called within the main module (not inside an I/O callback), which executes first?
- setImmediate() always runs first
- setTimeout() always runs first
- The order is non-deterministic (Correct answer)
- process.nextTick() must run before either
Correct answer: The order is non-deterministic
Outside an I/O cycle, the execution order of `setImmediate` vs `setTimeout(fn,0)` is non-deterministic due to timing constraints of the process.
Question 4: What does `require.resolve()` return without actually loading the module?
- The module's exports object
- The resolved filename/path of the module (Correct answer)
- A boolean indicating if the module exists
- The module's package.json contents
Correct answer: The resolved filename/path of the module
`require.resolve()` performs the module lookup algorithm and returns the resolved absolute file path without executing or caching the module.
Question 5: Which `os` module method returns the amount of free system memory in bytes?
- os.totalmem()
- os.freemem() (Correct answer)
- os.memory()
- os.availmem()
Correct answer: os.freemem()
`os.freemem()` returns the amount of free operating system memory in bytes at the time of the call.
Question 6: What is the purpose of the `domain` phase (idle/prepare) in the Node.js event loop?
- Executes DNS lookups
- Used internally by Node.js for internal operations before polling (Correct answer)
- Handles file descriptor events
- Processes nextTick callbacks
Correct answer: Used internally by Node.js for internal operations before polling
The idle and prepare phases are used internally by Node.js and libuv to prepare state before entering the poll phase.
Question 7: How do you remove all listeners for a specific event from an EventEmitter?
- emitter.off(event)
- emitter.removeListener(event)
- emitter.removeAllListeners(event) (Correct answer)
- emitter.clearListeners(event)
Correct answer: emitter.removeAllListeners(event)
`emitter.removeAllListeners(event)` removes all listeners for the specified event, or all events if no argument is passed.
Which Node.js core module allows you to create child processes?