Node.js Core Modules & Event Loop 2 — Questions and Answers
Question 1: Which phase of the Node.js event loop processes I/O callbacks that were deferred from the previous iteration?
- timers
- pending callbacks (Correct answer)
- poll
- check
Correct answer: pending callbacks
The 'pending callbacks' phase executes I/O callbacks deferred to the next loop iteration, such as TCP errors.
Question 2: What does `process.nextTick()` do relative to the event loop phases?
- Runs after the current poll phase
- Runs at the end of the current event loop iteration before moving to the next phase (Correct answer)
- Runs during the check phase
- Runs after all I/O callbacks
Correct answer: Runs at the end of the current event loop iteration before moving to the next phase
`process.nextTick()` callbacks execute after the current operation completes but before the event loop continues to the next phase.
Question 3: Which core module provides the `EventEmitter` class?
- events (Correct answer)
- stream
- util
- net
Correct answer: events
The `events` core module exports the `EventEmitter` class which is the foundation of Node.js event-driven architecture.
Question 4: What is the default maximum number of listeners for a single event on an EventEmitter before a warning is emitted?
- 5
- 10 (Correct answer)
- 20
- 100
Correct answer: 10
Node.js emits a memory leak warning when more than 10 listeners are added for a single event; this limit is configurable via `setMaxListeners()`.
Question 5: Which method of the `fs` module reads a file without buffering the entire content in memory?
- fs.readFile()
- fs.readFileSync()
- fs.createReadStream() (Correct answer)
- fs.open()
Correct answer: fs.createReadStream()
`fs.createReadStream()` returns a Readable stream that reads the file in chunks, avoiding loading the whole file into memory.
Question 6: What does the `path.resolve()` method return?
- A relative path from the current working directory
- An absolute path by processing the sequence of paths from right to left (Correct answer)
- The canonical path after resolving symlinks
- The directory name of the given path
Correct answer: An absolute path by processing the sequence of paths from right to left
`path.resolve()` processes path segments right-to-left, prepending the CWD if no absolute path is found, and returns an absolute path.
Question 7: In the Node.js event loop, the `check` phase is dedicated to executing callbacks registered by which function?
- setTimeout()
- setInterval()
- setImmediate() (Correct answer)
- process.nextTick()
Correct answer: setImmediate()
The `check` phase exclusively runs `setImmediate()` callbacks, which execute after the poll phase completes.
Which phase of the Node.js event loop processes I/O callbacks that were deferred from the previous iteration?