Node.js Technology & Digital Applications 2 — Questions and Answers
Question 1: Which Node.js global object provides information about the current process, such as environment variables and command-line arguments?
- global
- process (Correct answer)
- runtime
- system
Correct answer: process
The `process` global object exposes environment variables via `process.env`, CLI args via `process.argv`, and lifecycle methods like `process.exit()`.
Question 2: What is the primary purpose of the `cluster` module in Node.js?
- Manage database connection pools
- Share a single server port across multiple worker processes (Correct answer)
- Bundle JavaScript modules for the browser
- Synchronize file system operations
Correct answer: Share a single server port across multiple worker processes
The `cluster` module lets you fork child processes that share the same server port, enabling multi-core utilization.
Question 3: In Node.js streams, what does calling `stream.pipe(destination)` return?
- The source stream
- A Promise that resolves when piping is complete
- The destination stream (Correct answer)
- undefined
Correct answer: The destination stream
`pipe()` returns the destination stream, enabling chaining like `a.pipe(b).pipe(c)`.
Question 4: Which HTTP status code should a RESTful Node.js API return when a resource is successfully created?
- 200 OK
- 204 No Content
- 201 Created (Correct answer)
- 202 Accepted
Correct answer: 201 Created
201 Created signals that the request succeeded and a new resource was created as a result.
Question 5: What does the `--inspect` flag do when starting a Node.js application?
- Runs the app in strict mode
- Enables the V8 inspector protocol for debugging (Correct answer)
- Prints a dependency tree to stdout
- Validates all require() paths at startup
Correct answer: Enables the V8 inspector protocol for debugging
`--inspect` opens a debugging port (default 9229) so Chrome DevTools or VS Code can attach to the running process.
Question 6: Which built-in Node.js module would you use to generate cryptographically secure random bytes?
- Math
- random
- crypto (Correct answer)
- os
Correct answer: crypto
The `crypto` module provides `crypto.randomBytes()` for generating cryptographically strong pseudo-random data.
Question 7: What is the role of the `libuv` library in the Node.js architecture?
- Parses JavaScript source code into an AST
- Provides the event loop and async I/O abstractions across platforms (Correct answer)
- Manages npm package metadata
- Compiles TypeScript to JavaScript
Correct answer: Provides the event loop and async I/O abstractions across platforms
libuv is the C library that implements Node.js's event loop, thread pool, and cross-platform async I/O operations.
Which Node.js global object provides information about the current process, such as environment variables and command-line arguments?