How can you ensure a specific listener is called only once for an event in Node.js?

  • event.once(event, listener)
  • event.addOnceListener(event, listener)
  • listener.once(event)
  • event.addSingleListener(event, listener)
To ensure that a specific listener is called only once for an event in Node.js, you can use the event.once(event, listener) method. This method registers the listener to be invoked only once the next time the specified event is emitted. The other options do not provide this functionality.

How can you implement a custom event emitter using the Events module in Node.js?

  • By directly extending the 'EventEmitter' class.
  • By using a third-party library like 'emitter.js'.
  • By creating a regular JavaScript function with event handling capabilities.
  • By using promises and async/await.
To implement a custom event emitter in Node.js, you can create a new class that extends the built-in 'EventEmitter' class. This allows you to inherit all the event-related functionality and add custom events and event handling methods.

You need to implement a feature where documents from a MongoDB collection are automatically archived after a certain period. How would you approach this requirement using Mongoose functionalities?

  • Use a cron job to periodically scan and archive documents.
  • Modify the MongoDB collection to automatically archive documents on insertion.
  • Manually move documents to an archive collection after a certain period.
  • Implement a trigger to archive documents on update.
To automatically archive documents in MongoDB using Mongoose, you can use a cron job (Option 1). This allows for periodic scanning and archiving based on your criteria. Options 2, 3, and 4 are not typical approaches for automatic archiving and may not be as efficient or maintainable.

Which property of the response object is used to send an HTTP status code to the client?

  • statusCode
  • status
  • httpStatus
  • responseCode
To send an HTTP status code to the client in Node.js, you use the statusCode property of the response object. For example, response.statusCode = 200; sets the status code to 200 (OK). The other options are not standard properties for setting the status code.

You are tasked with optimizing CRUD operations on a legacy system experiencing slow Read operations due to large, unindexed tables. What approach would you take to optimize the Read operations without affecting the Write performance significantly?

  • Add appropriate indexes
  • Implement caching
  • Normalize the database
  • Increase server memory
To optimize Read operations in a legacy system with slow Read operations and large, unindexed tables, adding appropriate indexes (Option 1) is a common approach. It allows the database engine to locate and retrieve data more efficiently during Read operations without significantly impacting Write performance. Caching (Option 2) can further improve Read performance by storing frequently accessed data in memory. Normalization (Option 3) might help with data organization but doesn't directly address Read performance. Increasing server memory (Option 4) can enhance overall performance but may not specifically target Read optimization.

How can ESLint be configured to automatically fix certain issues when saving files?

  • Using the "fix-on-save" extension
  • Adding a "fix" key in the configuration file
  • Running ESLint with the --fix flag
  • Toggling the "Auto-Fix" setting in your code editor
ESLint can be configured to automatically fix certain issues when saving files by running ESLint with the --fix flag. This flag instructs ESLint to apply automatic fixes to the code, resolving issues such as indentation errors or missing semicolons. The other options are not standard methods for achieving automatic fixes with ESLint.

You are creating a Node.js library and want to ensure that it is compatible with specific versions of Node.js and npm. How would you specify this in your project to inform users?

  • Specify the Node.js and npm versions in the "engines" field of the package.json file.
  • Include a README.md file with compatibility information.
  • Add a comment in the main JavaScript file with compatibility details.
  • Create a separate compatibility.json file.
To inform users about the compatible Node.js and npm versions, you should specify them in the "engines" field of the package.json file. This ensures that users are aware of the required versions when they install your library. The other options are not standard practices for specifying compatibility.

You are developing a document management system with extensive text-based content. How would you implement indexing and full-text search to ensure efficient and relevant retrieval of documents based on user queries?

  • Use Inverted Indexes for Keywords and Implement a Full-Text Search Engine
  • Create a Single Index for All Document Metadata
  • Employ Simple SQL Queries for Text Retrieval
  • Disable Indexing for Better Write Performance
To efficiently retrieve documents in a text-based document management system, you should use Inverted Indexes for keywords and implement a Full-Text Search Engine. This approach allows users to search for documents based on their content efficiently. Creating a single index for all document metadata may not provide efficient text-based retrieval. Simple SQL queries may not handle complex text searches effectively. Disabling indexing entirely would severely impact search performance.

In the Read-Through caching strategy, if a requested data is not found in the cache, it is read from the ______ and then stored in the cache.

  • Database
  • File System
  • Network
  • RAM
In the Read-Through caching strategy, if the requested data is not found in the cache, it is read from the database and then stored in the cache for future use. This strategy helps reduce the load on the database by caching frequently accessed data.

To include a partial in a Pug template, you use the ______ keyword.

  • insert
  • include
  • partial
  • embed
In Pug, you include a partial using the include keyword. This allows you to reuse and insert content from other files into your Pug templates. The other options are not the standard way to include partials in Pug.

In Express.js, to handle errors in asynchronous functions, you should pass errors to ______.

  • next()
  • throw
  • res.send()
  • console.log()
In Express.js, to handle errors in asynchronous functions, you should pass errors to the next() function. This allows you to delegate error handling to custom error-handling middleware or the default Express error handler.

What would be the output of using the spread operator on an object with symbol properties?

  • It will throw an error because the spread operator cannot be used with objects containing symbol properties.
  • It will copy all properties except the symbol properties.
  • It will include the symbol properties in the spread.
  • It will create an empty object.
When using the spread operator on an object with symbol properties, it will copy all properties except the symbol properties. Symbols are not enumerable by default, so they are excluded when spreading an object.