Which of the following statements is true regarding JavaScript's symbol data type?
- Symbols can be used as property keys in objects, but they are not unique.
- Symbols are always visible in the global scope.
- Symbols are automatically garbage collected.
- Symbols are of the same data type as strings.
Symbols in JavaScript can be used as property keys in objects, and they are guaranteed to be unique. This uniqueness makes them useful for defining non-colliding property names.
What would be the output of typeof NaN in JavaScript?
- number
- null
- string
- undefined
The output of typeof NaN in JavaScript is 'number'. This might seem counterintuitive, but it's because NaN is considered a numeric data type in JavaScript.
You are developing a Node.js application that receives binary data from a TCP stream. Which Node.js construct would you use to handle this binary data efficiently?
- Buffer
- Array
- String
- Number
In Node.js, the Buffer construct is specifically designed for handling binary data efficiently. It allows you to work with raw binary data directly. The other options (Array, String, Number) are not suitable for handling binary data efficiently.
In NoSQL databases like MongoDB, how can you ensure data consistency across distributed databases?
- Use a two-phase commit protocol.
- Implement eventual consistency.
- Leverage distributed transactions.
- Use primary key constraints.
In NoSQL databases, data consistency across distributed databases is typically ensured by implementing eventual consistency. Two-phase commit protocols and distributed transactions can be complex and might introduce performance bottlenecks. Primary key constraints, while important, don't inherently guarantee distributed consistency.
You are creating a real-time chat application in Node.js where you need to broadcast binary data to multiple clients. How would you manage the binary data to ensure optimal performance and minimal memory usage?
- Use a binary WebSocket library
- Convert binary data to base64 before transmission
- Send binary data as raw bytes
- Serialize binary data as JSON
To ensure optimal performance and minimal memory usage when broadcasting binary data in a real-time chat application, it's best to send binary data as raw bytes. Using a binary WebSocket library or converting to base64 can introduce unnecessary overhead, and serializing binary data as JSON is not efficient.
When importing a module, what is the difference between a relative and absolute path?
- A relative path specifies the module's location relative to the current file, while an absolute path specifies the module's exact location in the file system.
- A relative path always begins with a forward slash (/), while an absolute path does not use any slashes.
- A relative path is used for built-in Node.js modules, while an absolute path is used for third-party modules.
- A relative path is used for server-side modules, while an absolute path is used for client-side modules.
In Node.js, when importing a module, you can use either a relative path or an absolute path. A relative path specifies the module's location relative to the current file, making it flexible for local module imports. An absolute path, on the other hand, specifies the exact file system path to the module.
What is the significance of the engines field in the package.json file of a Node.js project?
- It lists the development tools used for the project.
- It specifies the JavaScript engines that can run the project.
- It defines the project's build scripts.
- It lists the project's dependencies.
The engines field in package.json specifies the JavaScript engines and their versions that are compatible with the project. It helps ensure that the project runs correctly on the specified engines and prevents installation on incompatible engines. This is crucial for compatibility and stability.
What is a Closure in JavaScript?
- A function with no parameters.
- A function that can be called only once.
- A function that has access to its own scope, as well as the outer (enclosing) function's scope.
- A function that returns a random value.
In JavaScript, a closure is a function that has access to its own scope, as well as the outer (enclosing) function's scope. This allows it to capture and remember values from the outer function even after the outer function has finished executing.
How can you catch synchronous errors occurring in a route handler in Express?
- Using try-catch blocks
- Using the catchError method
- Adding the error event listener
- Using the next() function
You can catch synchronous errors occurring in a route handler in Express by using try-catch blocks. This allows you to capture errors and handle them within your route handler function. The other options are not the typical way to catch synchronous errors in Express.
In Semantic Versioning, what does a caret (^) symbol before a version number, like ^1.2.3, signify in terms of upgrades?
- It indicates that only bug fixes and patches can be upgraded.
- It allows upgrades to the specified version and any future backward-compatible versions.
- It signifies that downgrades are allowed.
- It prevents any upgrades to the version.
The caret (^) symbol signifies that you can upgrade to the specified version and any future backward-compatible versions, including new features and non-breaking changes.