1. Deep Event Loop Understanding
In a Node.js application, what happens if a long-running CPU-bound computation runs directly on the main thread? For example, if a for-loop runs for several seconds synchronously.
- A. Node.js will continue processing other client requests as usual.
- B. The Event Loop will be blocked, causing the application to become unresponsive.
- C. Node.js will automatically delegate the computation to a worker thread.
- D. An out-of-memory exception will be thrown immediately.
- E. The computation will be split into smaller asynchronous tasks automatically.
2. Core Modules Identification
Which of the following is NOT a built-in Node.js core module? For example, consider importing modules without external installation.
- A. http
- B. fs
- C. path
- D. express
- E. crypto
3. Stream API Complexity
Given a large file, what is the main benefit of using Node.js streams to read and process it compared to fs.readFile()? For instance, processing a 10GB log file.
- A. Streams read the entire file into memory at once, making processing faster.
- B. Streams allow for processing data in chunks, reducing memory usage.
- C. Streams automatically compress the file while reading.
- D. fs.readFile is faster and more memory-efficient than streams.
- E. Streams only work with text files.
4. Cluster Module Subtleties
What is the primary purpose of the cluster module in Node.js? For example, when building a web server handling thousands of concurrent users.
- A. To automatically balance HTTP requests across remote servers.
- B. To utilize multiple CPU cores by spawning child processes.
- C. To enable asynchronous I/O operations within a single thread.
- D. To reload code when changes are detected.
- E. To manage package dependencies between modules.
5. Asynchronous Patterns
Which of the following is the correct way to promisify a Node.js callback-based API? For example, converting fs.readFile to return a Promise.
- A. fs.readFileAsync(path)
- B. require('util').promisify(fs.readFile)
- C. fs.promisifyRead(path)
- D. readFile.promised(path)
- E. fs.promise.readFile(path)
6. Error Event Handling
What happens if an 'error' event emitted by a Node.js stream is not handled? For example, a readable stream that encounters a file read error.
- A. The process ignores the error silently.
- B. The error is automatically logged and the stream continues.
- C. The process crashes with an uncaught exception.
- D. The stream retries reading the file automatically.
- E. The error event is converted to a warning.
7. NPM Dependency Versioning
If a package.json lists a dependency as ^1.5.3, which of the following versions could NPM install? For example, when running npm install.
- A. 1.4.9
- B. 2.0.0
- C. 1.6.0
- D. 0.9.0
- E. 1.5.2
8. Process.nextTick Nuances
What is the function of process.nextTick() in the Node.js event loop? For example, when scheduling a callback after the current operation completes.
- A. It schedules a task immediately after I/O events.
- B. It schedules a task to execute at the start of the next Event Loop iteration.
- C. It schedules a task before any other asynchronous tasks, after the current operation.
- D. It is equivalent to setTimeout(fn, 0).
- E. It delays execution until all network I/O is finished.
9. OS Module Subtleties
Which method from the Node.js 'os' module returns the system uptime in seconds? For example, to print how long the system has been running since the last reboot.
- A. os.platform()
- B. os.sysUptime()
- C. os.uptime()
- D. os.time()
- E. os.tick()
10. Secure Coding Practices
Which of the following is the most secure way to prevent prototype pollution attacks when parsing JSON data in a Node.js application?
- A. Using JSON.parse() without any validation.
- B. Deleting __proto__ property after parsing.
- C. Validating incoming keys against an allowed whitelist before parsing.
- D. Using eval() to parse JSON after replacing dangerous keys.
- E. Converting the JSON to a string and back repeatedly.