Node.js Core Modules & Event Loop — Questions and Answers
Question 1: What is the event loop in Node.js?
- A mechanism that handles asynchronous operations by executing callbacks when I/O operations complete (Correct answer)
- A for loop that iterates over DOM events
- A browser API for handling click events
- A database query optimization technique
Correct answer: A mechanism that handles asynchronous operations by executing callbacks when I/O operations complete
The event loop is Node.js's core mechanism for non-blocking I/O. It continuously checks for pending callbacks, timers, and I/O operations, executing them when ready without blocking the main thread.
Question 2: What is the purpose of the 'fs' module in Node.js?
- To interact with the file system for reading, writing, and manipulating files (Correct answer)
- To format strings for display output
- To manage font styles in HTML output
- To filter and sort array elements
Correct answer: To interact with the file system for reading, writing, and manipulating files
The 'fs' (file system) module provides an API for interacting with the file system, including reading files, writing files, creating directories, and managing file permissions.
Question 3: What is the difference between 'process.nextTick()' and 'setImmediate()' in Node.js?
- process.nextTick() executes before any I/O events; setImmediate() executes after the current I/O poll phase (Correct answer)
- They are identical functions with different names
- setImmediate() always runs first
- process.nextTick() is deprecated in favor of setImmediate()
Correct answer: process.nextTick() executes before any I/O events; setImmediate() executes after the current I/O poll phase
process.nextTick() fires immediately after the current operation completes, before the event loop continues. setImmediate() fires on the next iteration of the event loop, after I/O events.
Question 4: What does 'require()' do in Node.js?
- Imports and caches a module, returning its exports (Correct answer)
- Creates a new HTTP request to an external API
- Declares a required parameter for a function
- Installs a package from npm
Correct answer: Imports and caches a module, returning its exports
require() loads a module (built-in, npm package, or local file), executes it, caches the result, and returns the module's exports object for use in the current file.
Question 5: What is the Buffer class used for in Node.js?
- To handle binary data directly without converting to strings (Correct answer)
- To buffer user interface updates in the browser
- To create HTML form input buffers
- To manage database connection pools
Correct answer: To handle binary data directly without converting to strings
The Buffer class provides a way to handle raw binary data in Node.js, essential for working with file systems, network protocols, and streams that deal with bytes rather than strings.
Question 6: What are Streams in Node.js and why are they important?
- Objects that enable reading or writing data in chunks, reducing memory usage for large datasets (Correct answer)
- CSS animation sequences rendered on the server
- Database query result sets stored in arrays
- Video streaming services built with Node.js
Correct answer: Objects that enable reading or writing data in chunks, reducing memory usage for large datasets
Streams process data piece by piece (in chunks) rather than loading entire datasets into memory, making them essential for handling large files, network data, and real-time processing efficiently.
What is the event loop in Node.js?