Welcome to Node JS Quiz for beginners!. This quiz consists of 30 MCQs (Multiple-Choice Questions) to test your knowledge of Node JS concepts and NPM commands.
Node.js is a cross-platform environment and library for running JavaScript applications which is used to create networking and server-side applications.
Note that each question is followed by the correct answer and an explanation to help reinforce your knowledge.
1. What is Node.js?
Answer:
Explanation:
Node.js is not a framework; it’s a runtime environment that allows JavaScript to be executed server-side.
2. Which package manager is bundled with Node.js by default?
Answer:
Explanation:
npm, which stands for Node Package Manager, comes bundled with Node.js and facilitates package management.
3. How can you initiate a project using npm?
Answer:
Explanation:
Using npm init, you can initialize a new project and create a package.json file.
4. Which method is used in Node.js to include modules?
Answer:
Explanation:
In Node.js, the require() method is used to import modules.
5. Which core module in Node.js provides asynchronous file operations?
Answer:
Explanation:
The fs module in Node.js is used for file operations, both synchronous and asynchronous.
6. In Node.js, which of the following is an in-built event emitter?
Answer:
Explanation:
EventEmitter is a part of the events module in Node.js and is used to handle custom events within apps.
7. Which global object provides functionality to control the Node.js runtime process?
Answer:
Explanation:
The process global object provides information about and control over the Node.js runtime process.
8. How can you read command-line arguments in a Node.js application?
Answer:
Explanation:
The process.argv property returns an array containing command-line arguments passed to the Node.js process.
9. Which method is used to create a new instance of a server in Node.js?
Answer:
Explanation:
The http.createServer() method from the http module is used to create a new server instance in Node.js.
10. Which of the following is a core module for handling paths?
Answer:
Explanation:
The path module provides utilities for working with file and directory paths.
11. What does the stream module in Node.js provide?
Answer:
Explanation:
The stream module provides the capability to handle streaming data, like reading from or writing to files in a continuous manner.
12. What is the Node.js event loop?
Answer:
Explanation:
The event loop in Node.js continuously checks the event queue and processes events (like function callbacks), allowing Node.js to be non-blocking and asynchronous.
13. Which utility allows you to automatically restart a Node application when changes are detected?
Answer:
Explanation:
Nodemon is a utility that monitors for any changes in your Node application and automatically restarts the server, making development more efficient.
14. What is a buffer in Node.js?
Answer:
Explanation:
In Node.js, a buffer is a temporary storage location for raw data before it gets processed or transferred to a different location.
15. Which Node.js method is used to delay the execution of a function?
Answer:
Explanation:
setTimeout() is a global method in Node.js (and in browsers) used to execute a function after a set number of milliseconds.
16. Which of the following allows Node.js to be scalable?
Answer:
Explanation:
Node.js’s event-driven, non-blocking I/O model allows it to be lightweight and efficient, making it scalable for real-time applications that run across distributed devices.
17. What is the main difference between exports and module.exports in Node.js?
Answer:
Explanation:
Both exports and module.exports point to the same object. However, if you reassign exports, it no longer references module.exports.
18. What would be the primary use case for the cluster module in Node.js?
Answer:
Explanation:
The cluster module allows Node.js applications to run on multiple CPU cores, ensuring better load balancing and improved application performance.
19. Which of the following is NOT a core module in Node.js?
Answer:
Explanation:
While express is a popular module used in Node.js for web server operations, it is not a core module. It has to be installed separately via npm.
20. Which method in the fs module is used to read a file asynchronously?
Answer:
Explanation:
fs.readFile() is the asynchronous method used for reading files in Node.js.
21. In which object are all the environment variables stored in a Node.js application?
Answer:
Explanation:
The process.env object contains the user environment in a Node.js application.
22. What does the eventEmitter.emit method in Node.js do?
Answer:
Explanation:
eventEmitter.emit is used to trigger or emit a particular event.
23. How do you install a package locally using npm in a Node.js application?
Answer:
Explanation:
Using npm install <package-name> installs the package locally in the node_modules directory of the current project.
24. In a Node.js application, which method is used to send a JSON response back from a server?
Answer:
Explanation:
In Express (a popular framework for Node.js), res.json() is used to send a JSON response to the client.
25. What does the npm init command do?
Answer:
Explanation:
npm init is used to set up a new or existing npm package, primarily creating a package.json file.
26. Which of the following is used to import modules in Node.js?
Answer:
Explanation:
In Node.js, the require function is used to import modules.
27. Which npm command is used to install all dependencies listed in the package.json file?
Answer:
Explanation:
The npm install command installs all the dependencies listed in the package.json file.
28. Which Node.js command is used to execute a JavaScript file, say “app.js”?
Answer:
Explanation:
To execute a file, you simply use the node followed by the filename.
29. What is the purpose of the –save flag in the npm install command?
Answer:
Explanation:
The –save flag is used to add the installed module to the package.json file’s dependencies.
30. Which npm command can be used to list all globally installed packages?
Answer:
Explanation:
The command npm list -g displays all globally installed npm packages.