Node.js Core Modules & Event Loop 5 — Questions and Answers
Question 1: What is the role of `libuv` in Node.js?
- A JavaScript parser used instead of V8
- A multi-platform support library that provides the event loop and async I/O (Correct answer)
- The module resolution algorithm implementation
- A built-in HTTP client library
Correct answer: A multi-platform support library that provides the event loop and async I/O
libuv is the C library that provides Node.js with its cross-platform event loop, thread pool, and asynchronous I/O capabilities.
Question 2: How many threads does the default libuv thread pool use?
- 1
- 2
- 4 (Correct answer)
- 8
Correct answer: 4
The default libuv thread pool size is 4, used for file system operations, DNS lookups, and other blocking tasks; it can be changed via `UV_THREADPOOL_SIZE`.
Question 3: What does the `stream.pipeline()` utility function do compared to manually piping streams?
- It buffers all data in memory before passing it on
- It automatically handles cleanup and error propagation across all streams in the pipeline (Correct answer)
- It converts streams to Promises automatically
- It only works with HTTP streams
Correct answer: It automatically handles cleanup and error propagation across all streams in the pipeline
`stream.pipeline()` properly handles errors and cleanup (destroy) for all streams in the chain, preventing the resource leaks that manual `.pipe()` can cause.
Question 4: Which core module would you use to parse and format URL strings in Node.js?
- querystring
- http
- url (Correct answer)
- path
Correct answer: url
The `url` module provides the WHATWG `URL` class and legacy `url.parse()`/`url.format()` functions for working with URL strings.
Question 5: When a Promise resolves inside a `setTimeout` callback, in what order do its `.then()` callbacks execute relative to other `setTimeout` callbacks?
- After all setTimeout callbacks in the same iteration
- Before the next event loop phase begins, as microtasks (Correct answer)
- In the check phase alongside setImmediate
- Immediately inline, synchronously
Correct answer: Before the next event loop phase begins, as microtasks
Resolved Promise `.then()` callbacks are microtasks and drain completely after each task (including setTimeout callbacks) before the event loop advances.
Question 6: What is the purpose of `module.exports` vs `exports` in Node.js CommonJS modules?
- They are identical and fully interchangeable in all cases
- `exports` is a shorthand reference to `module.exports`; reassigning `exports` breaks the link but reassigning `module.exports` works (Correct answer)
- `module.exports` is deprecated in favor of `exports`
- `exports` only works for functions, `module.exports` works for objects
Correct answer: `exports` is a shorthand reference to `module.exports`; reassigning `exports` breaks the link but reassigning `module.exports` works
`exports` is initially a reference to `module.exports`, but if you reassign `exports = something`, you break the reference and the module exports nothing new; you must use `module.exports =` for reassignment.
Question 7: Which Node.js `fs` method watches a file for changes and triggers a callback when the file is modified?
- fs.observe()
- fs.watch() (Correct answer)
- fs.monitor()
- fs.subscribe()
Correct answer: fs.watch()
`fs.watch()` uses OS-native file system events to watch a file or directory and invoke a callback when changes occur.
What is the role of `libuv` in Node.js?