














































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!















































































Question 1. What does the let keyword provide that var does not? A) Block‑level scope B) Function‑level scope C) Global scope only D) No hoisting Answer: A Explanation: let creates variables that are scoped to the nearest block, whereas var is function‑scoped. Question 2. Which array method returns a new array containing only the elements that satisfy a given predicate? A) reduce B) filter C) map D) some Answer: B Explanation: filter iterates over an array and includes elements for which the callback returns a truthy value. Question 3. In ES6, which syntax correctly creates a shallow copy of an object obj? A) Object.clone(obj) B) {...obj} C) obj.copy() D) Object.assign(null, obj) Answer: B Explanation: The spread operator {...obj} creates a new object with the same own enumerable properties (shallow copy).
Question 4. What will the following code output? console.log(typeof Symbol('id')); A) "symbol" B) "object" C) "string" D) "function" Answer: A Explanation: Symbol values have the primitive type "symbol". Question 5. Which of the following correctly defines a class method that does not need to be bound to an instance? A) static greet() {} B) greet = () => {} C) function greet() {} D) this.greet = function() {} Answer: A Explanation: static methods belong to the class itself and can be called without an instance. Question 6. What is the purpose of the new.target meta‑property inside a constructor? A) To reference the prototype of the object being created B) To detect if the constructor was called with new C) To return the current instance D) To access the parent class constructor Answer: B Explanation: new.target is undefined when a function is called without new; otherwise it points to the constructor.
Question 10. What does the pipeline utility from the stream module help prevent? A) Memory leaks caused by unhandled errors in streams B) Duplicate data being written to a file C) Automatic conversion of binary data to strings D) Overwriting existing files Answer: A Explanation: pipeline automatically forwards errors and ensures proper cleanup of streams, reducing the risk of memory leaks. Question 11. Which of the following is true about backpressure in Node.js streams? A) It occurs when the writable side processes data faster than the readable side produces it B) It is automatically handled by the pipe method, which pauses the source when the destination is saturated C) It can only be managed by manually calling stream.pause() and stream.resume() on the readable stream D) Backpressure only applies to Transform streams Answer: B Explanation: pipe respects backpressure by pausing the source stream when the destination cannot keep up. Question 12. Which of these is a correct way to create a Transform stream that converts input to uppercase? A) new stream.Transform({ transform(chunk, enc, cb) { cb(null, chunk.toString().toUpperCase()); } }) B) new stream.Transform({ read(size) { this.push(data.toUpperCase()); } }) C) new stream.Transform({ write(chunk, enc, cb) { this.push(chunk.toUpperCase()); cb(); } }) D) new stream.Transform({ transform(chunk) { return chunk.toUpperCase(); } })
Answer: A Explanation: The transform method receives a chunk, encoding, and callback; calling the callback with the transformed data pushes it downstream. Question 13. What is the output of the following code? console.log(Buffer.from([0x41, 0x42, 0x43]).toString('hex')); A) "ABC" B) "414243" C) "0x41 0x42 0x43" D) "A B C" Answer: B Explanation: toString('hex') converts each byte to its two‑digit hexadecimal representation, concatenated as a string. Question 14. Which of the following best describes the difference between a Readable and a Duplex stream? A) Readable can only emit data; Duplex can both emit and consume data B) Readable is synchronous; Duplex is asynchronous C) Readable works only with strings; Duplex works only with Buffers D) Readable is a subclass of Duplex Answer: A Explanation: A Duplex stream implements both the readable and writable interfaces, allowing two‑way data flow. Question 15. In Node.js, which method is used to create a writable stream that appends to a file instead of overwriting it? A) fs.createWriteStream(path, { flags: 'w' }) B) fs.createWriteStream(path, { flags: 'a' })
B) Attaching .catch() to the returned promise C) Ignoring the promise entirely D) Using await fn().then() without a catch block Answer: C Explanation: Ignoring a promise that may reject leads to unhandled rejections, which is unsafe. Question 19. When using async/await, which of the following statements is true about error propagation? A) Errors thrown inside an awaited promise are automatically caught by the nearest catch block B) Errors must be manually wrapped in try/catch for each await C) await suppresses all errors and returns undefined D) Only syntax errors are caught, runtime errors are not Answer: A Explanation: If an awaited promise rejects, the rejection is thrown as an exception, which can be caught by a surrounding try/catch. Question 20. Which Node.js core module provides the EventEmitter class? A) events B) stream C) process D) util Answer: A Explanation: The events module exports EventEmitter, the base class for event‑driven patterns. Question 21. What does the fs.promises.readFile method return?
A) The file contents as a Buffer synchronously B) A promise that resolves with the file contents C) An EventEmitter that emits a data event D) The number of bytes read Answer: B Explanation: The promise‑based API returns a Promise that resolves to a Buffer (or string if an encoding is provided). Question 22. Which flag passed to fs.open opens a file for reading and writing, creating it if it does not exist? A) 'r' B) 'w' C) 'a' D) 'wx+' Answer: D Explanation: 'wx+' opens the file for reading and writing, fails if the file exists (x), and creates it if not. Question 23. In the path module, which method normalizes a path by resolving .. and . segments? A) path.resolve() B) path.normalize() C) path.join() D) path.parse() Answer: B Explanation: path.normalize cleans up redundant . and .. segments without resolving to an absolute path.
Explanation: process.exit forces immediate termination with the given exit code; pending I/O is not awaited. Question 27. Which of the following signals is emitted when a user presses Ctrl+C in the terminal? A) SIGTERM B) SIGKILL C) SIGINT D) SIGHUP Answer: C Explanation: Ctrl+C generates the SIGINT (interrupt) signal. Question 28. When using child_process.spawn, which option ensures that the child’s stdout is streamed as a readable stream rather than buffered? A) { stdio: 'inherit' } B) { stdio: ['pipe', 'pipe', 'pipe'] } C) { detached: true } D) { shell: true } Answer: B Explanation: The default stdio configuration (pipe) creates streams for stdin, stdout, and stderr, allowing data to be read incrementally. Question 29. What is the primary difference between exec and spawn in the child_process module? A) exec returns a stream; spawn returns a buffer B) exec buffers the entire output in memory; spawn streams data piece‑by‑piece C) exec can only run shell built‑ins; spawn can run any executable
D) exec runs asynchronously; spawn runs synchronously Answer: B Explanation: exec collects stdout and stderr into buffers, which can cause memory issues for large outputs; spawn provides streams. Question 30. Which method is used to send a message from a parent process to a forked child process? A) child.send(message) B) child.write(message) C) process.emit('message', message) D) process.postMessage(message) Answer: A Explanation: When using fork, a communication channel is established, and child.send transmits JSON‑serializable messages. Question 31. In a forked child process, which event listener receives messages from the parent? A) process.on('data', ...) B) process.on('message', ...) C) process.on('signal', ...) D) process.on('error', ...) Answer: B Explanation: The child process listens for 'message' events to receive data sent via child.send. Question 32. What does the --inspect flag enable when running a Node.js application? A) Strict mode for all modules B) Remote debugging using Chrome DevTools or other inspectors
A) class ValidationError extends Error { constructor(msg) { super(msg); this.name = 'ValidationError'; } } B) function ValidationError(msg) { this.message = msg; } C) class ValidationError { constructor(msg) { this.message = msg; } } D) class ValidationError extends Exception {} Answer: A Explanation: Extending Error and calling super ensures proper stack trace and prototype chain; setting name customizes the error type. Question 36. Which NPM command adds a package as a development dependency? A) npm install <pkg> --save B) npm install <pkg> --save-dev C) npm add <pkg> - D D) npm install <pkg> - g Answer: B Explanation: --save-dev (or -D) records the package under devDependencies in package.json. Question 37. What does the ^ caret symbol mean in a package.json version range, e.g., "express": "^4.17.1"? A) Only version 4.17.1 is allowed B) Any version greater than or equal to 4.17.1 but less than 5.0.0 C) Any version greater than 4.17.1 including major releases D) The latest version regardless of major version Answer: B Explanation: The caret allows updates that do not change the left‑most non‑zero digit (major version for 4.x).
Question 38. Which of the following is true about the CommonJS require cache? A) Modules are cached only when require is called with an absolute path B) The cache can be cleared by deleting the entry from require.cache C) Each call to require always reloads the module from disk D) The cache is shared across different Node.js processes automatically Answer: B Explanation: require.cache holds loaded modules; removing an entry forces a fresh load on the next require. Question 39. Which command runs an npm script named test defined in package.json? A) npm run test B) npm test C) Both A and B D) npm execute test Answer: C Explanation: npm test is a shortcut for npm run test; both invoke the script. Question 40. What does npm audit do? A) Checks for outdated dependencies only B) Scans the dependency tree for known security vulnerabilities and suggests fixes C) Formats package.json according to style guidelines D) Runs unit tests automatically Answer: B Explanation: npm audit contacts the npm registry’s vulnerability database and reports issues.
Explanation: Returning a Promise allows the test runner to await its resolution, indicating asynchronous completion. Question 44. Which of the following is NOT a standard Node.js test runner? A) Mocha B) Jest C) Jasmine D) node test (built‑in) Answer: D Explanation: Node.js does not ship a dedicated test runner; frameworks like Mocha, Jest, and Jasmine are external. Question 45. What is the purpose of the --trace-warnings flag when running a Node.js application? A) It treats all warnings as errors and aborts execution B) It prints a stack trace for each warning emitted C) It disables all warning messages D) It logs warnings to a file named warnings.log Answer: B Explanation: --trace-warnings adds a stack trace to each warning, aiding debugging. Question 46. In a Node.js REPL, which command clears the console screen? A) .clear B) .cls C) Ctrl+L D) Both A and C Answer: D
Explanation: .clear is a REPL command; Ctrl+L also clears the terminal in most shells. Question 47. Which of the following correctly imports the fs/promises API using ES modules? A) import fs from 'fs/promises'; B) const fs = require('fs/promises'); C) import { promises as fs } from 'fs'; D) import * as fs from 'fs/promises'; Answer: A Explanation: fs/promises is an ES module entry point; default import works as shown. Question 48. What does the path.extname('/foo/bar.txt') call return? A) '.txt' B) 'txt' C) '/bar.txt' D) '' Answer: A Explanation: extname returns the period followed by the file extension. Question 49. Which method can be used to watch for changes to a file or directory? A) fs.watchFile B) fs.readFile C) fs.statSync D) fs.open Answer: A Explanation: fs.watchFile polls the file for changes and triggers a callback.
Explanation: process.argv contains the executable path, script path, and subsequent arguments; slicing from index 2 drops the first two. Question 53. Which of the following statements about process.nextTick is accurate? A) It schedules a callback to run after the current poll phase but before any I/O callbacks B) It runs after all timers have fired C) It is equivalent to setImmediate D) It can only be used inside async functions Answer: A Explanation: process.nextTick callbacks are processed immediately after the current operation, before the event loop continues. Question 54. Which option disables TLS certificate verification for HTTPS requests made with the built‑in https module? A) { rejectUnauthorized: false } in the request options B) { verify: false } C) process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0' D) Both A and C Answer: D Explanation: Both setting rejectUnauthorized: false on the request and the environment variable disable verification (the latter affects all HTTPS requests). Question 55. What does the cluster module primarily provide? A) A way to spawn child processes that share server ports for load balancing B) A high‑level HTTP client library C) Automatic code transpilation from ES6 to ES D) A built‑in test runner
Answer: A Explanation: cluster enables creating worker processes that can share the same network socket, improving concurrency. Question 56. Which of the following correctly creates a new worker thread using the worker_threads module? A) new Worker('./worker.js') after const { Worker } = require('worker_threads'); B) new Thread('./worker.js') C) fork('./worker.js') D) spawn('node', ['./worker.js']) Answer: A Explanation: The Worker class from worker_threads runs a separate V8 instance in a new thread. Question 57. In a worker thread, which event is emitted when the parent sends a message? A) 'message' B) 'data' C) 'signal' D) 'error' Answer: A Explanation: Workers use the 'message' event to receive data from the parent via parentPort.postMessage. Question 58. What does the util.promisify function do? A) Converts a callback‑style function into one that returns a Promise B) Turns a Promise into a callback function C) Serializes a function for inter‑process communication