When would you use export default over named exports in a module?
- export default is used when you want to export multiple values from a module as an object with named keys.
- export default is used when you want to export a single value, function, or class from a module.
- export default is used when you want to export a module without specifying a name for it.
- export default is used when you want to create a private module that can't be imported from other modules.
export default is used when you want to export a single value, function, or class as the default export of a module. This allows you to import it using any name you prefer when importing. Named exports are used when you want to export multiple values with specific names.
What does the return statement do in a JavaScript function?
- Returns a value from the function and exits the function
- Declares a variable
- Creates a loop
- Includes a comment in the code
The return statement in a JavaScript function is used to return a value from the function and immediately exit the function. It is used to send data back to the caller of the function. The other options do not describe the purpose of the return statement.
How can you remove a listener from an event using the Events module in Node.js?
- event.removeListener(event, listener)
- event.remove(listener)
- event.off(event, listener)
- listener.remove(event)
To remove a listener from an event using the Events module in Node.js, you should use the event.removeListener(event, listener) method. This method takes the event name and the listener function as arguments and removes the specified listener from the event. The other options are not valid methods for removing listeners.
You are integrating ESLint into a legacy project. How would you approach linting the existing codebase without disrupting the development workflow?
- Lint the entire codebase in one go and fix all issues immediately.
- Gradually introduce ESLint, starting with new code and addressing legacy code issues incrementally.
- Disable ESLint for the legacy code and only apply it to new code.
- Ignore linting in legacy projects as it can't be easily integrated.
To avoid disruption, it's best to gradually introduce ESLint. Start by applying it to new code and address legacy code issues incrementally. Linting the entire codebase at once might be overwhelming and disruptive. Disabling ESLint for legacy code or ignoring it is not a recommended approach for maintaining code quality.
You are developing an application with multiple user roles, and each role has different levels of access to resources. How would you securely implement role-based access control to prevent unauthorized access?
- Use JSON Web Tokens (JWT) to manage user sessions and roles.
- Implement access control lists (ACLs) in your application.
- Check the user's role in the frontend to determine access.
- Rely solely on server-side sessions to control access.
Option (1) is correct. Using JWTs for session management and roles is a secure approach as they are self-contained and can be verified without relying on server-side sessions. Options (2) and (4) are less secure and may lead to vulnerabilities. Option (3) is incorrect as access control should be enforced on the server.
The fs.createReadStream method is particularly useful when dealing with ______ sized files.
- small
- large
- binary
- text
The fs.createReadStream method in Node.js is particularly useful when dealing with large sized files. This method allows you to efficiently read large files in smaller chunks, which can be memory-efficient for handling large datasets.
What happens to the Event Loop when the callback queue and the task queue are both not empty?
- It suspends the Event Loop until both queues are empty.
- It processes callbacks from the callback queue before tasks from the task queue.
- It processes tasks from the task queue before callbacks from the callback queue.
- It randomly selects items to process from both queues.
When both the callback queue and the task queue are not empty, the Event Loop in Node.js follows a specific order. It first processes callbacks from the callback queue before moving to tasks from the task queue. This order ensures that callback functions, which often include I/O operations, are handled promptly.
In JavaScript, a function that is defined inside another function has access to the outer function's variables, forming a ________.
- Closure
- Enclosure
- Junction
- Intersection
In JavaScript, when a function is defined inside another function, it forms a "closure." A closure allows the inner function to access the outer function's variables even after the outer function has finished executing.
You are designing a system with heavy Read and Update operations on the database. Which database design strategy would you adopt to balance the load and ensure data consistency?
- Sharding
- Replication
- Denormalization
- Indexing
In a scenario with heavy Read and Update operations, sharding is a database design strategy where you distribute the data across multiple servers or nodes. This helps balance the load and improve Read performance. Sharding ensures data consistency by managing data distribution and replication across shards. Replication (Option 2) can improve Read performance but doesn't address balancing the load directly. Denormalization (Option 3) can help with Reads but may complicate data consistency. Indexing (Option 4) helps with Read performance but doesn't balance the load.
Which of the following is true regarding object keys in JavaScript?
- Object keys must be unique within an object.
- Object keys can only be of type string.
- Object keys are case-sensitive.
- Object keys can only be integers.
Object keys in JavaScript are case-sensitive. This means that keys with different letter casing (e.g., 'key' and 'Key') are considered distinct in an object. The other options do not accurately describe the behavior of object keys.