In Node.js, the maximum number of listeners that can be added to an EventEmitter by default is ______.
- 10
- 100
- 50
- Infinity
In Node.js, the maximum number of listeners that can be added to an EventEmitter by default is 10. This is to prevent potential memory leaks caused by adding too many listeners inadvertently.
The ______ script is executed automatically before the npm pack and npm publish commands, but not when installing local dependencies.
- build
- prepublish
- prepack
- postpack
The prepublish script is executed automatically before the npm pack and npm publish commands, but it is not triggered when installing local dependencies using npm install. This script is commonly used for tasks like code compilation or testing before publishing.
When building a RESTful API with Express, how can you ensure that the API supports versioning?
- Include the version in the API's route
- Use a version control system like Git
- Use a separate server for each version
- Add version information in the API response headers
To support versioning in a RESTful API built with Express, it's common to include the version in the API's route. For example, /v1/resource and /v2/resource for different versions. The other options are not standard practices for API versioning.
Which of the following is a common use case for OAuth?
- Storing user passwords securely
- Generating random API keys
- Allowing third-party applications to access user data
- Creating secure database connections
OAuth is commonly used to allow third-party applications to access user data without exposing the user's credentials. It is a widely adopted protocol for authorization in the context of web applications and APIs. The other options do not represent typical use cases for OAuth.
In the context of web development, what is the main difference between authentication and authorization?
- Authentication verifies the identity of a user, while authorization determines what actions or resources a user can access.
- Authentication and authorization are the same concepts.
- Authentication ensures the confidentiality of data, while authorization ensures data integrity.
- Authentication validates the user's email, while authorization checks their password.
In web development, the main difference between authentication and authorization is that authentication verifies the identity of a user, while authorization determines what actions or resources a user can access after their identity is established. Authentication focuses on "who you are," while authorization focuses on "what you are allowed to do." The other options do not accurately describe the distinction between these two concepts.
Your team is tasked with optimizing a large-scale e-commerce application that experiences sporadic spikes in traffic. Which set of optimizations would be most effective in handling high traffic while maintaining a responsive user experience?
- Implement caching for frequently accessed data
- Use a single server instance to minimize resource usage
- Disable load balancing to simplify the architecture
- Optimize for low-bandwidth connections only
Implementing caching for frequently accessed data is an effective optimization strategy for handling high traffic while maintaining a responsive user experience. Caching can reduce the load on your backend servers and speed up response times. Using a single server instance and disabling load balancing may lead to performance bottlenecks and decreased reliability. Optimizing for low-bandwidth connections alone may not address the scalability and responsiveness issues during traffic spikes.
What is a closure in JavaScript?
- A closure is a built-in JavaScript function
- A closure is a function that has access to the variables from its containing function, even after the containing function has finished executing.
- A closure is a special type of variable in JavaScript
- A closure is a function that is defined within another function.
A closure in JavaScript is a function that has access to the variables from its containing function, even after the containing function has finished executing. This is a fundamental concept in JavaScript for maintaining data encapsulation and is often used for creating private variables and functions.
How can a method be added to an object's prototype?
- object.prototype.addMethod(methodName, method)
- prototype.addMethod(object, methodName, method)
- Object.prototype.methodName = method
- object.methodName = method
To add a method to an object's prototype in JavaScript, you can assign the method to the prototype property of the constructor function that created the objects. For example, Object.prototype.methodName = method. This way, all objects created from that constructor will inherit the method. The other options do not follow the correct syntax for adding a method to a prototype.
You are building a web application and want to ensure the UI components render correctly under different states. How would you leverage Mocha or Jest to validate the component’s output under various states?
- Use Jest snapshots to capture component output in different states and automatically compare them.
- Manually inspect the component's HTML output and compare it to expected output in each test case.
- Don't test UI components; focus on testing business logic.
- Use Mocha's built-in UI testing features to validate rendering.
To validate UI components under different states, you can use Jest snapshots. Snapshots capture the component's output and allow you to automatically compare it to expected output, making it efficient and reliable. The other options are less efficient or do not focus on UI component testing.
When using the fs module, the ______ event is emitted when there is an error reading or writing a file.
- error
- read
- write
- event
When using the fs (File System) module in Node.js, the error event is emitted when there is an error reading or writing a file. This event is crucial for handling errors when working with files using Node.js.