How can you create a deep copy of an object in JavaScript?

  • JSON.parse(JSON.stringify(object))
  • object.copy()
  • deepCopy(object)
  • Object.clone(object)
To create a deep copy of an object in JavaScript, you can use JSON.parse(JSON.stringify(object)). This method serializes the object to a JSON string and then parses it back into a new object, effectively creating a deep copy. The other options are not valid for deep copying objects.

In Node.js, what is the purpose of the process.on('uncaughtException', handler) method?

  • To catch exceptions and continue program execution
  • To exit the Node.js process on any unhandled exception
  • To display uncaught exceptions in the console
  • To handle exceptions within a specific function
The process.on('uncaughtException', handler) method in Node.js is used to capture unhandled exceptions and prevent the Node.js process from crashing. It allows you to specify a custom handler function to handle these exceptions gracefully. The other options do not accurately describe the purpose of this method.

In a NoSQL database like MongoDB, how are schemas defined and enforced?

  • Schemas are strictly enforced before data insertion
  • Schemas are defined using SQL
  • Schemas are flexible and can evolve with data
  • Schemas are predefined and cannot change
In NoSQL databases like MongoDB, schemas are flexible and can evolve with data. There's no strict enforcement of schemas before data insertion, allowing for schema-less data storage. This flexibility is one of the key features of NoSQL databases, making them suitable for handling unstructured or semi-structured data.

How does JavaScript’s prototypal inheritance differ from classical inheritance models?

  • JavaScript uses delegation-based inheritance, where objects inherit from other objects directly, not from classes.
  • JavaScript uses class-based inheritance like many other programming languages.
  • JavaScript's inheritance is more rigid and less flexible compared to classical models.
  • JavaScript's inheritance is limited to only built-in objects.
JavaScript's prototypal inheritance differs from classical inheritance models by using delegation-based inheritance. Instead of inheriting from classes, objects inherit directly from other objects, which is more flexible and dynamic.

You are working on a project where you need to load different modules based on user actions dynamically. What approach should you take to load the modules only when necessary?

  • Use synchronous module loading.
  • Use Webpack to bundle all modules upfront.
  • Employ dynamic import statements.
  • Load all modules at application startup.
To load modules dynamically based on user actions, you should employ dynamic import statements (Option c). Dynamic imports allow you to load modules asynchronously and only when they are needed, improving the efficiency of your application. Synchronous loading (Option a) and loading all modules upfront (Option d) are counterproductive for this scenario. While Webpack bundling (Option b) can be useful for other purposes, it doesn't address the need for dynamic loading.

You are tasked with developing a file upload module in Node.js. The files uploaded by users are binary data. How would you handle the incoming binary data to ensure data integrity and optimal resource utilization?

  • Use streams to handle and process binary data in chunks
  • Read the entire binary data into memory before processing
  • Convert binary data to hexadecimal for storage
  • Store binary data in a JSON format
To ensure data integrity and optimal resource utilization when handling binary data in a file upload module, it's best to use streams to handle and process binary data in chunks. This approach minimizes memory usage and ensures data integrity. Reading the entire binary data into memory is not efficient, and the other options are not suitable for storing binary data.

You are tasked with evolving the schema of a critical, high-traffic database while minimizing downtime and data inconsistency. What strategies would you employ to safely apply schema migrations?

  • Use database migration tools with rollback support.
  • Apply schema changes during off-peak hours.
  • Utilize database versioning and backward-compatible changes.
  • Pause the database for maintenance during schema updates.
Option (1) is a common approach as it allows you to apply changes with rollback capabilities. Option (2) is valid to minimize the impact on users. Option (3) is also important, using versioning and backward-compatible changes ensures a smoother transition. Option (4) is typically not recommended, as it causes downtime.

In what scenarios would you implement custom middleware instead of using built-in middleware in Express.js?

  • Custom middleware should always be avoided, and built-in middleware should be used.
  • Custom middleware is necessary when working with database connections.
  • Custom middleware should be used when you need to perform application-specific logic that isn't covered by built-in middleware.
  • Custom middleware is used only for unit testing purposes.
Custom middleware is implemented in Express.js when you have specific application logic that cannot be handled by built-in middleware. Common scenarios include authentication, request logging, data validation, and handling application-specific errors. Built-in middleware covers common use cases, but custom middleware allows you to tailor the middleware pipeline to your application's unique needs.

How does normalizing database tables impact the Read and Update operations in CRUD?

  • Improves Read performance, may complicate Updates
  • Improves Update performance, may slow down Reads
  • Has no impact on either Read or Update operations
  • Improves both Read and Update operations
Normalizing database tables often improves Read performance by reducing data redundancy. However, it may complicate Update operations since data may be spread across multiple tables, requiring joins. Option 2 is not generally true as normalization tends to have a positive impact on Update operations. Option 4 is an oversimplification.

What does the resolve function do in a JavaScript Promise?

  • It is called when an error occurs.
  • It is called when the Promise is rejected.
  • It is called when the Promise is fulfilled with a value.
  • It is called to cancel the Promise.
The resolve function in a JavaScript Promise is called when the Promise is fulfilled successfully with a value. It signifies that the asynchronous operation inside the Promise has completed successfully, and the value provided in the resolve function is the result.