Node.js Technology & Digital Applications 5 — Questions and Answers
Question 1: What is the output of `console.log(0.1 + 0.2 === 0.3)` in Node.js?
- true
- false (Correct answer)
- NaN
- TypeError
Correct answer: false
Due to IEEE 754 floating-point representation, `0.1 + 0.2` equals `0.30000000000000004`, not exactly `0.3`, so the comparison is `false`.
Question 2: Which npm script name is automatically executed when you run `npm test`?
- "testing"
- "test" (Correct answer)
- "spec"
- "check"
Correct answer: "test"
`npm test` looks for and runs the `"test"` script defined in the `scripts` section of `package.json`.
Question 3: What does enabling `WAL` (Write-Ahead Logging) mode in SQLite provide when used with a Node.js application?
- Automatic database schema migrations
- Better concurrent read performance without blocking writes (Correct answer)
- Encrypted storage for sensitive data
- Automatic backups to cloud storage
Correct answer: Better concurrent read performance without blocking writes
WAL mode allows concurrent reads alongside a write without blocking, improving throughput in Node.js apps where reads are frequent.
Question 4: In Node.js, what is the difference between `setImmediate()` and `setTimeout(fn, 0)`?
- They are identical in behavior
- `setImmediate()` fires after I/O events in the current loop; `setTimeout(fn,0)` fires in the timers phase with a minimum ~1ms delay (Correct answer)
- `setImmediate()` runs in a worker thread; `setTimeout` runs on the main thread
- `setTimeout(fn,0)` has higher priority than `setImmediate()`
Correct answer: `setImmediate()` fires after I/O events in the current loop; `setTimeout(fn,0)` fires in the timers phase with a minimum ~1ms delay
`setImmediate()` is designed to run after I/O callbacks in the check phase, while `setTimeout(fn, 0)` runs in the timers phase with a minimum delay, making their order context-dependent.
Question 5: What is the purpose of the `helmet` middleware package in an Express.js application?
- Rate limiting incoming requests
- Setting secure HTTP response headers to protect against common web vulnerabilities (Correct answer)
- Encrypting the request body payload
- Managing session cookies securely
Correct answer: Setting secure HTTP response headers to protect against common web vulnerabilities
`helmet` sets HTTP headers like `Content-Security-Policy`, `X-Frame-Options`, and `X-XSS-Protection` to mitigate common attacks.
Question 6: How does Node.js handle uncaught exceptions by default?
- It logs the error and continues running
- It prints the stack trace and terminates the process (Correct answer)
- It restarts the process automatically
- It sends the error to all connected clients
Correct answer: It prints the stack trace and terminates the process
By default, an uncaught exception prints the stack trace to stderr and exits the process with code 1.
Question 7: Which method on a Node.js `Buffer` object converts its contents to a UTF-8 string?
- buffer.parse()
- buffer.decode()
- buffer.toString() (Correct answer)
- buffer.stringify()
Correct answer: buffer.toString()
`buffer.toString('utf8')` (or simply `buffer.toString()` since UTF-8 is the default encoding) converts the Buffer to a string.
What is the output of `console.log(0.1 + 0.2 === 0.3)` in Node.js?