Node.js Core Modules & Event Loop 4 — Questions and Answers
Question 1: Which `crypto` module function creates a Hash object used for computing cryptographic digests?
- crypto.hash()
- crypto.createHash() (Correct answer)
- crypto.digest()
- crypto.newHash()
Correct answer: crypto.createHash()
`crypto.createHash(algorithm)` returns a Hash instance; you call `.update()` with data and `.digest()` to get the result.
Question 2: What does `util.promisify()` do?
- Converts a Promise-based function to callback style
- Wraps a callback-style function to return a Promise (Correct answer)
- Makes any function run asynchronously
- Adds error handling to synchronous functions
Correct answer: Wraps a callback-style function to return a Promise
`util.promisify()` takes a function following the Node.js error-first callback convention and returns a Promise-returning version.
Question 3: What is a 'starvation' risk when using `process.nextTick()` recursively?
- It causes a stack overflow
- It can prevent the event loop from ever reaching the I/O poll phase (Correct answer)
- It blocks the garbage collector
- It increases memory fragmentation
Correct answer: It can prevent the event loop from ever reaching the I/O poll phase
Recursive `process.nextTick()` calls keep adding to the nextTick queue, potentially preventing the event loop from advancing to process I/O or timers.
Question 4: In Node.js, what is `__dirname`?
- The current working directory of the Node.js process
- The absolute path of the directory containing the currently executing file (Correct answer)
- The root directory of the project
- An alias for process.cwd()
Correct answer: The absolute path of the directory containing the currently executing file
`__dirname` is a module-scoped variable that holds the absolute path of the directory where the current module file resides.
Question 5: Which method of the `net` module creates a TCP server?
- net.Server()
- net.connect()
- net.createServer() (Correct answer)
- net.listen()
Correct answer: net.createServer()
`net.createServer([options][, connectionListener])` creates a new TCP server and optionally attaches a connection event listener.
Question 6: What is the significance of the `poll` phase in the Node.js event loop?
- It polls for DNS responses
- It retrieves new I/O events and executes their callbacks, blocking if necessary (Correct answer)
- It polls for timer expiry
- It processes microtasks
Correct answer: It retrieves new I/O events and executes their callbacks, blocking if necessary
The poll phase retrieves new I/O events and executes associated callbacks; if no timers are scheduled, it can block here waiting for I/O.
Question 7: What does `EventEmitter.once()` do differently from `EventEmitter.on()`?
- It registers a listener that fires only one time and then automatically removes itself (Correct answer)
- It registers a listener with higher priority
- It makes the listener synchronous
- It prevents other listeners from being called
Correct answer: It registers a listener that fires only one time and then automatically removes itself
`once()` registers a one-time listener that is automatically removed after it fires for the first time, unlike `on()` which fires on every emission.
Which `crypto` module function creates a Hash object used for computing cryptographic digests?