0% found this document useful (0 votes)
29 views4 pages

Node.js MCQs: 25 Essential Questions

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views4 pages

Node.js MCQs: 25 Essential Questions

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Node.

js MCQs (Questions 1–25)

1. What is [Link]?
○ A) A web browser
○ B) A JavaScript runtime built on Chrome's V8 JavaScript engine
○ C) A front-end framework
○ D) A database management system
2. Which command is used to initialize a new [Link] project?
○ A) node init
○ B) npm start
○ C) npm init
○ D) node create
3. Which built-in module is used to create a server in [Link]?
○ A) url
○ B) fs
○ C) http
○ D) events
4. Which method is used to read the contents of a file in [Link]?
○ A) [Link]()
○ B) [Link]()
○ C) [Link]()
○ D) [Link]()
5. What is npm in [Link]?
○ A) Node Package Manager
○ B) Node Process Manager
○ C) New Package Manager
○ D) None of the above
6. In [Link], which of the following is used to import a module?
○ A) import
○ B) require
○ C) load
○ D) fetch
7. Which of the following statements is true about the process object in [Link]?
○ A) It is a global object that provides information about the current process
○ B) It is only accessible in specific modules
○ C) It only works with the http module
○ D) It is used to manage file systems
8. How do you make an HTTP request in [Link]?
○ A) Using the https module only
○ B) Using the http or https modules
○ C) Using the server module
○ D) Using the request module only
9. Which of the following is NOT a [Link] core module?
○ A) fs
○ B) express
○ C) http
○ D) url
10. Which of the following methods is used to write data to a file in [Link]?
○ A) [Link]()
○ B) [Link]()
○ C) [Link]()
○ D) writeData()
11. What does the fs module stand for?
○ A) File Store
○ B) File System
○ C) Function Set
○ D) File Structure
12. What is the default scope for variables in [Link]?
○ A) Block
○ B) Global
○ C) Function
○ D) Local
13. Which command is used to install a package globally in [Link]?
○ A) npm install package-name
○ B) npm install -g package-name
○ C) npm global package-name
○ D) install package-name globally
14. Which function is used to register an event in [Link]?
○ A) [Link]()
○ B) on()
○ C) emit()
○ D) trigger()
15. How can you handle errors in [Link]?
○ A) Using try-catch blocks
○ B) Using error-first callbacks
○ C) Using the throw keyword
○ D) All of the above
16. Which of the following allows [Link] to handle multiple requests at the same
time?
○ A) Multi-threading
○ B) Asynchronous event-driven architecture
○ C) Synchronous blocking
○ D) None of the above
17. Which object is used to handle streams in [Link]?
○ A) stream
○ B) fs
○ C) buffer
○ D) data
18. What is the buffer in [Link] used for?
○ A) To handle file system operations
○ B) To handle raw binary data
○ C) To create HTTP servers
○ D) To parse JSON data
19. What does __dirname represent in [Link]?
○ A) The name of the current directory
○ B) The absolute path of the directory containing the current file
○ C) The path to the [Link] installation
○ D) The path to the node_modules folder
20. Which of the following is correct syntax for creating a simple server in [Link]?
○ A) [Link](function(req, res) { ... })
○ B) [Link](function(req, res) { ... })
○ C) [Link](function(req, res) { ... })
○ D) [Link](function(req, res) { ... })
21. How can you create a child process in [Link]?
○ A) Using child_process.fork()
○ B) Using spawn command
○ C) Using exec command
○ D) All of the above
22. Which of the following will display the version of [Link] installed?
○ A) node --v
○ B) node -version
○ C) node -v
○ D) node version
23. In [Link], what does the readable event indicate for a stream?
○ A) Data is ready to be read
○ B) Stream is closed
○ C) Data has been successfully written
○ D) Stream is paused
24. Which module is used to create a WebSocket server in [Link]?
○ A) http
○ B) ws
○ C) web
○ D) socket
25. What is the purpose of the path module in [Link]?
○ A) To manage HTTP requests
○ B) To work with directory and file paths
○ C) To handle API requests
○ D) To process JSON data

Common questions

Powered by AI

Despite Node.js’s single-thread model, concurrency is achieved by using the ‘child_process’ module which allows the running of JavaScript operations concurrently via sub-processing. Child processes can be created using methods like 'spawn', 'exec', or 'fork'. These processes can perform tasks asynchronously in parallel with the main execution thread, leveraging multitasking without blocking the main thread, thus enhancing performance and scalability .

The core difference between using the 'http' and 'ws' modules in Node.js lies in their protocol support. The 'http' module is used for creating HTTP servers which handle requests over the HTTP/HTTPS protocols, suitable for traditional web services. In contrast, the 'ws' module is used to create WebSocket servers which facilitate bi-directional full-duplex communication channels over a single TCP connection, ideal for real-time applications like chat applications .

The 'require' function in Node.js is used for module management and contributes by importing modules, JSON, or local files into the application. It reads a JavaScript file, executes it, and then returns the module's exports object. This mechanism allows for code modularization, enabling code separation and reuse across different parts of an application .

The 'path' module in Node.js is used for working with file and directory paths. It provides utilities that simplify the manipulation and creation of paths across different operating systems by normalizing paths and resolving relative paths to absolute paths. Functions from this module, such as 'path.join', 'path.resolve', and 'path.basename', are vital in building cross-platform file systems by ensuring consistent path transformation and concatenation .

npm, Node Package Manager, is a critical component of the Node.js ecosystem. It is used to manage and install packages/modules from the npm registry. This functionality is significant as it allows developers to easily share and reuse code, thus fostering a large community of modules contributing to rapid development and innovation within the Node.js ecosystem .

The 'http' module in Node.js provides the ability to transfer data over the Hyper Text Transfer Protocol (HTTP). It facilitates server creation by providing functions that create server instances capable of listening to ports and handling incoming HTTP requests. The typical use case involves using the 'http.createServer' method to start an HTTP server, enabling the handling of HTTP requests and responses seamlessly .

A developer might choose to install a package globally in Node.js when they need the package to be available across multiple projects, or when using command-line tools provided by the package. The command used for global installation is 'npm install -g package-name', which installs the package globally, making it accessible from the command line interface regardless of the current project directory .

'fs.readFile' is an asynchronous method used to read the contents of a file, meaning it doesn't block the execution thread while the file is being read. In contrast, 'fs.readFileSync' is synchronous and blocks the thread until the file is entirely read. The asynchronous method ('fs.readFile') is preferred for its non-blocking performance, ideal for scalable applications, whereas 'fs.readFileSync' can lead to performance bottlenecks due to its blocking nature .

Node.js manages multiple I/O requests simultaneously using an asynchronous event-driven architecture rather than multi-threading. This is supported by the Node.js single-threaded event loop that allows non-blocking I/O operations. As a result, Node.js can process other tasks while waiting for I/O operations to complete, effectively managing multiple requests in parallel .

Node.js ensures security in buffer management by carefully managing memory allocation and providing APIs that prevent common vulnerabilities such as buffer overflows. The 'Buffer' class in Node.js safely handles raw binary data based on the underlying memory buffer, with built-in safeguards to prevent memory breaches, such as boundary checks to keep operations within limits .

You might also like