Node.js Beginner Quiz
30 questions on Node.js Basics.
Question 1: Which command initializes a new Node.js project, creating a package.json file?
- A. npm install
- B. npm start
- C. npm init β (correct answer)
- D. node init
Explanation: The npm init command initializes a project by walking you through package configurations and outputting the initial package.json file.
Question 2: What engine does Node.js use to compile and execute JavaScript code?
- A. SpiderMonkey
- B. JavaScriptCore
- C. Google V8 β (correct answer)
- D. Chakra
Explanation: Node.js compiles and executes JavaScript code at extreme speeds using Google's open-source high-performance V8 engine, which is written in C++ and also powers Google Chrome.
Question 3: Which object represents the current Node.js execution process, allowing you to access environment variables?
- A. window
- B. process β (correct answer)
- C. system
- D. global
Explanation: The global process object provides control over the Node.js runtime environment. Environment variables are accessed via process.env.
Question 4: What will be the output of running this Node.js script?
``javascript
console.log(__filename);
``
- A. A string representation of "filename"
- B. The absolute file path of the current executing JavaScript file β (correct answer)
- C. Undefined
- D. Compiler Error
Explanation: In CommonJS modules, Node.js provides the __filename global variable, which returns the absolute system path of the current running script.
Question 5: Which core module is used to handle file path operations (joining, parsing) in Node.js?
- A. fs
- B. path β (correct answer)
- C. url
- D. file
Explanation: The path module provides utilities for working with file and directory paths. A common method is path.join(), which resolves cross-platform paths elegantly.
Question 6: Which keyword is used to import modules in standard CommonJS format?
- A. import
- B. require β (correct answer)
- C. include
- D. load
Explanation: In CommonJS, which is Node.js's traditional default module format, the global require() function is used to load and run other JavaScript files or packages.
Question 7: Which core module is used to read, write, and manage files on the system disk?
- A. path
- B. file
- C. fs β (correct answer)
- D. system
Explanation: The fs (File System) module implements standard CRUD operations on local files, providing both asynchronous callbacks, promise wrappers, and synchronous method blocks.
Question 8: How do you install an npm package (e.g. express) and save it to the dependencies list in package.json?
- A.
npm install express β (correct answer)
- B.
node install express
- C.
npm save express
- D.
npm get express
Explanation: Running npm install <package-name> (shorthand npm i) downloads the package files into the node_modules folder and automatically logs the package as a dependency inside package.json.
Question 9: Which standard object serves as the parent container for global variables in Node.js, replacing the browser's window object?
- A. window
- B. global β (correct answer)
- C. document
- D. process
Explanation: In Node.js, the global namespace is managed by the global object. Variables declared on global are available across all module files.
Question 10: What will be the output of this code snippet?
``javascript
const fs = require('fs');
fs.readFile('test.txt', 'utf8', (err, data) => {
if (err) {
console.log("Error");
} else {
console.log(data);
}
});
``
- A. Reads and prints the text contents of 'test.txt' asynchronously β (correct answer)
- B. Reads file synchronously
- C. Compiler Error
- D. White Screen
Explanation: fs.readFile reads a file asynchronously. It takes a callback function signature (err, data). The encoding parameter 'utf8' guarantees the binary buffer is parsed to a string.
Question 11: Which built-in module is used to create basic HTTP web servers in Node.js?
- A. net
- B. url
- C. http β (correct answer)
- D. express
Explanation: The built-in http module is the low-level utility used to establish servers and listen to network request payloads on specific ports.
Question 12: How do you export a function named greet in CommonJS format?
- A.
export greet;
- B.
module.exports = { greet }; β (correct answer)
- C.
exports.default = greet;
- D.
exports = greet;
Explanation: CommonJS modules export APIs by assigning values to module.exports or attaching properties directly to the exports object reference.
Question 13: Which popular third-party framework is used to build robust, streamlined REST APIs and web servers on top of Node's low-level HTTP module?
- A. Angular
- B. Express β (correct answer)
- C. React
- D. Laravel
Explanation: Express is a minimal, flexible Node.js web application framework that provides robust routing architectures and middleware capabilities for APIs.
Question 14: What is a middleware function in Express?
- A. A function that connects database pools
- B. A function that has access to request and response objects, capable of executing logic, terminating the cycle, or passing execution using
next() β (correct answer)
- C. A system compiler
- D. A server wrapper
Explanation: Express middleware are functions executed during request routing. They can modify request bodies, validate authentication, or terminate the pipeline before the route handler is called.
Question 15: Which folder contains all the downloaded npm dependencies and packages inside a Node.js project?
- A. bin
- B. node_modules β (correct answer)
- C. vendor
- D. lib
Explanation: The node_modules directory is generated automatically when running npm install. It holds all libraries and recursive sub-dependencies loaded by your project.
Question 16: What is the difference between fs.readFile and fs.readFileSync?
- A.
fs.readFile is asynchronous and non-blocking; fs.readFileSync is synchronous and blocks program execution until the operation completes β (correct answer)
- B. They are identical
- C.
fs.readFileSync is faster
- D.
fs.readFile is deprecated
Explanation: Synchronous operations block Node's single-threaded event loop, meaning no other requests can be handled while the file reads. Asynchronous options allow other requests to execute concurrently.
Question 17: What will be the output of the following Node.js script?
``javascript
console.log("Start");
setTimeout(() => {
console.log("Timer");
}, 0);
console.log("End");
``
- A. Start Timer End
- B. Start End Timer β (correct answer)
- C. Timer Start End
- D. Compiler Error
Explanation: The event loop places setTimeout callbacks into the task queue. The call stack completes all synchronous code (Start, End) before pulling tasks from the queue, even with 0ms delay.
Question 18: Which core Node.js module provides the EventEmitter class, used to implement event-driven pub-sub architectures?
- A. fs
- B. path
- C. events β (correct answer)
- D. stream
Explanation: The events module exposes the EventEmitter class, which allows components to bind listeners using on() and publish events using emit().
Question 19: How do you read environment variables safely in Node.js?
- A. process.env.VARIABLE_NAME β (correct answer)
- B. process.VARIABLE_NAME
- C. environment.VARIABLE_NAME
- D. env.VARIABLE_NAME
Explanation: All environment configurations defined on the host system or loaded via files are stored inside the process.env key-value collection.
Question 20: Which tool compiles and auto-restarts a Node.js server automatically upon editing script files in a development workspace?
- A. node
- B. nodemon β (correct answer)
- C. npm run
- D. webpack
Explanation: nodemon is a popular utility tool that monitors the directory files and restarts the server immediately when changes are detected, saving manual restart time.
Question 21: What is the purpose of the npm shrinkwrap or package-lock.json file in Node.js?
- A. To minify package code files
- B. To lock exact versions of all installed dependencies and tree structures, guaranteeing consistent environment installs β (correct answer)
- C. To compile JS modules
- D. To define environmental ports
Explanation: package-lock.json locks the specific dependency versions fetched during install, preventing minor version mismatch bugs when building on different servers.
Question 22: Which parser middleware is required in Express to receive and parse JSON request bodies inside req.body?
- A. express.json() β (correct answer)
- B. body-parser.urlencoded()
- C. express.parse()
- D. JSON.parse()
Explanation: In modern Express (v4.16+), express.json() is a built-in middleware that parses incoming payloads with JSON headers, populating req.body.
Question 23: What is a Stream in Node.js?
- A. A class inheritance model
- B. A collection of asynchronous event handlers that transfer data sequentially in chunks without holding the entire file in memory β (correct answer)
- C. A database connection pool
- D. A server network route
Explanation: Streams are perfect for reading massive files or media. Instead of reading an entire 1GB file into RAM, data is read and transmitted chunk-by-chunk.
Question 24: What does the single-threaded nature of Node.js mean?
- A. It can only handle one network client request at a time
- B. It executes JavaScript instructions on a single main thread, delegating heavy operations like disk read/write to system threads in the background β (correct answer)
- C. It compiles code slowly
- D. It only operates on single core servers
Explanation: JavaScript execution is single-threaded. However, Node uses C++ background worker pools (libuv) to perform non-blocking I/O concurrently.
Question 25: What is NPM?
- A. Node Parameter Manager
- B. Node Package Manager β (correct answer)
- C. Network Protocol Module
- D. New Package Method
Explanation: NPM is the default package manager for Node.js, providing a command-line utility and holding the world's largest registry of open-source libraries.
Question 26: What library does Node.js rely on under the hood to handle multi-threaded asynchronous I/O and the event loop?
- A. Babel
- B. libuv β (correct answer)
- C. V8
- D. Webpack
Explanation: libuv is a multi-platform support library written in C++ that handles thread pools, file system polling, and the primary execution of Node's event loop.
Question 27: What is the output?
``javascript
const path = require('path');
console.log(path.join('/user', 'local', '..', 'bin'));
``
- A. "/user/local/../bin"
- B. "/user/bin" β (correct answer)
- C. "/user/local/bin"
- D. Compiler Error
Explanation: path.join resolves relative segment references. '..' moves one directory level up, navigating from /user/local back to /user, then appending /bin.
Question 28: Which method is used in Express to trigger a HTTP redirect response to a different URL?
- A. res.redirect() β (correct answer)
- B. res.send("Redirect")
- C. req.redirect()
- D. res.location()
Explanation: The res.redirect('url') helper in Express sets the status code to 302 and updates the Location header to trigger browser navigation.
Question 29: What is the difference between setImmediate(callback) and process.nextTick(callback)?
- A.
process.nextTick executes immediately after current synchronous code block before event loop moves; setImmediate executes on next loop cycle β (correct answer)
- B. They are identical
- C.
setImmediate runs faster
- D.
process.nextTick is deprecated
Explanation: process.nextTick() callbacks are resolved immediately after the active operation completes, bypassing the event loop cycle queue. setImmediate() places callbacks on the check queue.
Question 30: What is Node.js buffer class?
- A. A styling container
- B. A class used to allocate raw memory to handle binary data streams directly β (correct answer)
- C. A database caching model
- D. A server middleware
Explanation: Since JavaScript historically had no representation for binary files, Node.js implements the Buffer class to read and handle streams of raw octets.