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.
How can you use the fs module to copy a file from one location to another?
- fs.copyFile()
- fs.moveFile()
- fs.rename()
- fs.copy()
To copy a file from one location to another using the fs module, you can use the fs.copyFile() method. This method allows you to copy the contents of one file to another location. The other options (fs.moveFile(), fs.rename(), and fs.copy()) serve different purposes in file manipulation but are not specifically for copying a file to another location.
When creating a mock object, what is typically expected regarding the behavior of the methods?
- They should behave exactly like the real methods.
- They should have no behavior.
- They should behave in a simplified or controlled manner.
- They should behave randomly.
When creating a mock object in unit testing, you typically expect its methods to behave in a simplified or controlled manner. Mock objects are used to simulate the behavior of real objects for testing purposes, so their methods should provide predictable responses that help you test specific scenarios. Options (1) and (4) are not typical expectations for mock methods. Option (2) would render the mock object useless for testing.
How can you destructure nested properties in objects using JavaScript?
- const { prop1.prop2 } = myObject;
- const { prop1: { prop2 } } = myObject;
- const [ prop1, prop2 ] = myObject;
- const { prop1[prop2] } = myObject;
To destructure nested properties in JavaScript objects, you should use the syntax in Option 2. It allows you to access nested properties within myObject. The other options either contain incorrect syntax or won't work for this purpose.
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.
What is the output of the following code snippet: console.log(1 == '1')?
- TRUE
- FALSE
- undefined
The output of the code snippet is true. In JavaScript, the == operator performs type coercion, so it converts the string '1' to a number before comparing, and 1 is equal to 1.
What is the primary use of Streams in Node.js?
- Managing database connections
- Handling HTTP requests and responses
- Storing configuration data
- Sorting arrays
The primary use of Streams in Node.js is for handling data in a streaming fashion, especially for reading and writing data efficiently. They are commonly used for handling HTTP requests and responses, file I/O, and more, where data can be processed in smaller chunks without loading the entire dataset into memory.