Node JS MCQs with answer

What is Node.js?
a) A web browser
b) A server-side JavaScript runtime
c) A database
d) A markup language

Answer: b) A server-side JavaScript runtime

2. Which platform is Node.js built on?
a) Java
b) .NET
c) Chrome's V8 JavaScript engine
d) Python

Answer: c) Chrome's V8 JavaScript engine

3. What is npm in the context of Node.js?
a) Node Package Manager
b) Node Programming Module
c) New Programming Mechanism
d) Node Project Manager

Answer: a) Node Package Manager

4. Which of the following modules is commonly used for handling file operations in Node.js?
a) fs
b) http
c) net
d) path

Answer: a) fs

5. What command is used to install a Node.js package globally?
a) npm add
b) npm install -g
c) npm global
d) npm setup

Answer: b) npm install -g

6. What is the purpose of the require function in Node.js?
a) To define a new variable
b) To include external modules
c) To declare a function
d) To create a new file

Answer: b) To include external modules

7. Which of the following is an HTTP framework for Node.js?
a) Express
b) Angular
c) React
d) Vue

Answer: a) Express

8. What is the role of the process object in Node.js?
a) Handling file operations
b) Managing network requests
c) Controlling the Node.js process
d) Parsing JSON data

Answer: c) Controlling the Node.js process

9. What does the term "callback" refer to in the context of Node.js?
a) A function passed as an argument to another function
b) A module for handling database queries
c) A built-in method for asynchronous tasks
d) A type of error in Node.js

Answer: a) A function passed as an argument to another function

10. How do you handle errors in Node.js asynchronous code?
a) Using try-catch blocks
b) Using the throw statement
c) Using callback functions
d) Using the catch method

Answer: c) Using callback functions

11. What is the purpose of the module.exports object in Node.js?
a) To import external modules
b) To export variables and functions from a module
c) To create a new module
d) To include built-in modules

Answer: b) To export variables and functions from a module

12. Which of the following is a package manager alternative to npm in Node.js?
a) Bower
b) Yarn
c) Grunt
d) Gulp

Answer: b) Yarn

13. What does the term "event loop" refer to in Node.js?
a) A loop for iterating through arrays
b) A loop for handling HTTP requests
c) A mechanism for handling events asynchronously
d) A loop for reading and writing files

Answer: c) A mechanism for handling events asynchronously 

14. What is the role of the Buffer class in Node.js?
a) Handling file operations
b) Reading user input
c) Storing binary data
d) Controlling network requests

Answer: c) Storing binary data

15. Which module is used for creating a web server in Node.js?
a) net
b) http
c) fs
d) path

Answer: b) http

16. How can you install a specific version of a Node.js package using npm?
a) npm install package@version
b) npm install package-version
c) npm install -v package
d) npm install package --version

Answer: a) npm install package@version

17. What is the purpose of the __dirname variable in Node.js?
a) To store the current module's filename
b) To get the current working directory of the script
c) To represent the main application directory
d) To reference the parent directory

Answer: b) To get the current working directory of the script

18. Which of the following is a valid way to import the fs module in Node.js?
a) import fs from 'fs';
b) const fs = require('fs');
c) include fs from 'fs';
d) const fs = import('fs');

Answer: b) const fs = require('fs');

19. How can you handle concurrent requests in Node.js?
a) Using multiple threads
b) Using synchronous code
c) Using the async keyword
d) Using the event-driven model

Answer: d) Using the event-driven model 

20. What is the purpose of the body-parser middleware in Express.js?
a) To parse JSON data in the request body
b) To handle HTTP headers
c) To encrypt data
d) To set response status codes

Answer: a) To parse JSON data in the request body

21. Which of the following is the correct way to install Express.js using npm?

a) npm install express
b) npm install express.js
c) npm add express
d) npm setup express

Answer: a) npm install express

22. What is the purpose of the next function in Express.js middleware?
a) To end the request-response cycle
b) To pass control to the next middleware function
c) To redirect the client to another route
d) To handle errors

Answer: b) To pass control to the next middleware function

23. Which HTTP method is used to update data in a RESTful API?
a) GET
b) POST
c) PUT
d) DELETE

Answer: c) PUT

24. What is the role of the dotenv module in a Node.js application?
a) To handle environment variables
b) To encrypt data
c) To parse JSON data
d) To create a server

Answer: a) To handle environment variables

25. How can you handle cross-origin resource sharing (CORS) in an Express.js application?
a) By using the cross-origin middleware
b) By setting the Access-Control-Allow-Origin header
c) By using the cors middleware
d) By configuring the server to allow all origins

Answer: c) By using the `cors` middleware

26. In Node.js, what is the purpose of the child_process module?
a) To create child threads
b) To spawn child processes
c) To manage child modules
d) To handle child exceptions

Answer: b) To spawn child processes

27. What is the purpose of the WebSocket protocol in Node.js?

a) To handle HTTP requests
b) To enable real-time communication between a client and server
c) To serve static files
d) To secure data transmission

Answer: b) To enable real-time communication between a client and server

28. Which of the following is a templating engine commonly used with Express.js?
a) Pug
b) Jade
c) EJS
d) Handlebars

Answer: c) EJS

29. How do you set up a basic HTTP server in Node.js without using any external framework?
a) Using the http.createServer method
b) Using the express module
c) Using the server module
d) Using the http.server method

Answer: a) Using the `http.createServer` method

30. What is the purpose of the cookie-parser middleware in Express.js?
a) To parse HTTP cookies
b) To encrypt cookies
c) To set response status codes
d) To handle session data

Answer: a) To parse HTTP cookies

31. What is the purpose of the mongoose module in a Node.js application?
a) To create middleware functions
b) To connect to a MongoDB database
c) To handle HTTP requests
d) To encrypt data

 Answer: b) To connect to a MongoDB database

32. Which of the following is true about the event-driven model in Node.js?
a) It uses multiple threads to handle events
b) It is synchronous in nature
c) It is based on the publish-subscribe pattern
d) It does not support events

Answer: c) It is based on the publish-subscribe pattern 

33. What is the purpose of the npm init command?
a) To initialize a new Node.js project
b) To install Node.js packages
c) To start a new Node.js server
d) To create a new npm user

Answer: a) To initialize a new Node.js project

34. How do you handle query parameters in an Express.js route?
a) Using the query object
b) Using the params object
c) Using the body object
d) Using the request object

Answer: d) Using the `request` object

35. What is the purpose of the helmet middleware in an Express.js application?
a) To handle HTTP headers
b) To encrypt data
c) To set response status codes
d) To secure the application by setting various HTTP headers

Answer: d) To secure the application by setting various HTTP headers 

36. What is the role of the passport module in an Express.js application?
a) To handle authentication
b) To serve static files
c) To encrypt data
d) To create middleware functions

Answer: a) To handle authentication

37. Which of the following modules can be used for handling asynchronous operations in Node.js?

a) async
b) sync
c) await
d) defer

Answer: a) async

38. How do you handle routing in an Express.js application?

a) Using the route object
b) Using the express-routing module
c) Using the app.route method
d) Using the route method

Answer: c) Using the `app.route` method

39. What is the purpose of the Promise object in Node.js?

a) To handle HTTP requests
b) To handle asynchronous operations
c) To create middleware functions
d) To serve static files

Answer: b) To handle asynchronous operations 

40. Which of the following is a built-in module in Node.js for handling URL-related tasks?

a) url
b) path
c) querystring
d) request

Answer: a) url

41. How can you terminate a Node.js process?

a) node.terminate();
b) process.terminate();
c) exit();
d) process.exit();

Answer: d) process.exit();

42. What is the purpose of the app.use method in Express.js?
a) To define routes
b) To configure middleware
c) To handle errors
d) To set up a database connection

Answer: b) To configure middleware

43. Which module can be used for creating a secure HTTPS server in Node.js?
a) https
b) secure
c) ssl
d) tls

Answer: a) https

44. What is the role of the process.env object in Node.js?

a) To manage environment variables
b) To control the Node.js process
c) To store global variables
d) To handle file operations

Answer: a) To manage environment variables 

45. How do you install a specific version of Node.js using nvm (Node Version Manager)?
a) nvm install node@version
b) nvm install version
c) nvm use version
d) nvm install node/version

Answer: b) nvm install version

46. Which module is commonly used for testing in a Node.js application?
a) jest
b) mocha
c) chai
d) test.js

Answer: b) mocha

47. What is the purpose of the pm2 module in Node.js?
a) To manage Node.js processes
b) To create middleware functions
c) To handle HTTP requests
d) To serve static files

Answer: a) To manage Node.js processes

48. What is the purpose of the multer middleware in Express.js?
a) To handle HTTP headers
b) To parse JSON data
c) To process multipart/form-data for file uploads
d) To encrypt data

Answer: c) To process multipart/form-data for file uploads

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

77236