Node JS Quiz - MCQ - Multiple Choice Questions

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?

A. A front-end framework
B. A back-end framework
C. A JavaScript runtime built on Chrome's V8 JavaScript engine
D. A type of database

Answer:

C. A JavaScript runtime built on Chrome's V8 JavaScript engine

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?

A. npm
B. bower
C. yarn
D. grunt

Answer:

A. npm

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?

A. npm start
B. npm init
C. npm run
D. npm create

Answer:

B. npm init

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?

A. require()
B. include()
C. import
D. fetch()

Answer:

A. require()

Explanation:

In Node.js, the require() method is used to import modules.

5. Which core module in Node.js provides asynchronous file operations?

A. http
B. url
C. fs
D. path

Answer:

C. fs

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?

A. EventEmitter
B. Events
C. EventLoop
D. EventYield

Answer:

A. EventEmitter

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?

A. Global
B. Runtime
C. Process
D. NodeControl

Answer:

C. Process

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?

A. argv
B. process.args
C. arguments
D. process.argv

Answer:

D. process.argv

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?

A. http.createServer()
B. http.createInstance()
C. http.newServer()
D. http.createServerInstance()

Answer:

A. http.createServer()

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?

A. url
B. dir
C. path
D. location

Answer:

C. path

Explanation:

The path module provides utilities for working with file and directory paths.

11. What does the stream module in Node.js provide?

A. Tools for creating WebSockets
B. Utilities for handling HTTP operations
C. A way to handle streaming data
D. Functions for dealing with promises

Answer:

C. A way to handle streaming data

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?

A. A loop that emits custom events
B. A loop that continuously checks for events and processes them
C. A mechanism to define custom events
D. The main process of a Node.js application

Answer:

B. A loop that continuously checks for events and processes them

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?

A. NodeLive
B. NodeReload
C. Nodemon
D. AutoNode

Answer:

C. Nodemon

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?

A. A temporary storage spot for data being transferred
B. A type of variable
C. A mechanism to control data flow
D. A module for handling binary data

Answer:

A. A temporary storage spot for data being transferred

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?

A. process.delay()
B. setTimeout()
C. wait()
D. sleep()

Answer:

B. setTimeout()

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?

A. Multithreading
B. Event-driven architecture
C. Larger memory allocation
D. High CPU usage

Answer:

B. Event-driven architecture

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?

A. They are the same.
B. exports is for functions, while module.exports is for objects.
C. exports is a reference to module.exports.
D. module.exports is the legacy way to export modules.

Answer:

C. exports is a reference to module.exports.

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?

A. Data validation
B. Improved logging
C. Load balancing over multiple CPU cores
D. Data encryption

Answer:

C. Load balancing over multiple CPU cores

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?

A. fs
B. http
C. express
D. url

Answer:

C. express

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?

A. fs.readFile()
B. fs.readSync()
C. fs.openFile()
D. fs.read()

Answer:

A. fs.readFile()

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?

A. env
B. process.env
C. node.env
D. app.env

Answer:

B. process.env

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?

A. It sets up a new event.
B. It triggers an event.
C. It listens for an event.
D. It terminates an ongoing event.

Answer:

B. It triggers an event.

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?

A. npm install -g <package-name>
B. npm local install <package-name>
C. npm run install <package-name>
D. npm install <package-name>

Answer:

D. npm install <package-name>

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?

A. res.json()
B. res.sendJSON()
C. res.returnJSON()
D. res.pushJSON()

Answer:

A. res.json()

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?

A. It installs all the dependencies listed in package.json.
B. It starts the Node.js application.
C. It creates a new Node.js application.
D. It initializes a new package.json file.

Answer:

D. It initializes a new package.json file.

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?

A. import { module } from 'module-name'
B. #include 'module-name'
C. require('module-name')
D. using module-name

Answer:

C. require('module-name')

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?

A. npm init
B. npm run
C. npm start
D. npm install

Answer:

D. npm install

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"?

A. node install app.js
B. node run app.js
C. node app.js
D. npm app.js

Answer:

C. node app.js

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?

A. To globally install a package
B. To save a backup of the current project
C. To save the package version in the package-lock.json
D. To save the package in the package.json dependencies

Answer:

D. To save the package in the package.json dependencies

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?

A. npm list -g
B. npm global list
C. npm show
D. npm modules

Answer:

A. npm list -g

Explanation:

The command npm list -g displays all globally installed npm packages.


Comments