How can the process object be used to handle application termination in Node.js?
- process.exit()
- process.end()
- process.terminate()
- process.halt()
In Node.js, you can use process.exit() to gracefully terminate a Node.js application. It allows you to specify an exit code. The other options, such as process.end(), process.terminate(), and process.halt(), are not valid methods for application termination.
Which of the following is a primary use case for stubbing in tests?
- Capturing and verifying function calls
- Simulating the behavior of a function or method
- Mocking external dependencies
- Generating random test data
Stubbing in tests is often used to simulate the behavior of a function or method, allowing you to control its responses for testing purposes. It's especially useful when you want to isolate the code under test and ensure that certain functions return specific values or execute specific code paths.
How can you ensure the security of file uploads in a web application?
- Use an unauthenticated endpoint
- Limit file size
- Disallow specific file types
- Perform server-side validation
To ensure the security of file uploads in a web application, it's crucial to perform server-side validation. This includes checking the file's content type, verifying its size, and possibly scanning it for malware. Other important security measures include limiting file sizes to prevent denial-of-service attacks and disallowing specific file types to prevent the upload of potentially harmful files. Using an unauthenticated endpoint is not recommended as it could lead to security vulnerabilities.
In JavaScript, altering an object’s prototype at runtime can lead to ______ performance impacts.
- Positive
- Negative
- No
- Minimal
In JavaScript, altering an object's prototype at runtime can lead to "Negative" performance impacts. Modifying prototypes at runtime can cause performance bottlenecks, as it can affect the behavior of all objects that share the same prototype. It's generally recommended to avoid such runtime modifications for performance reasons.
What is the difference between the == and === operators in JavaScript?
- == compares values for equality, === compares values and types for equality
- == compares values and types for equality, === compares values for equality
- == is used for assignment, === is used for comparison
- == performs a deep comparison, === performs a shallow comparison
In JavaScript, == (loose equality) compares values for equality but performs type coercion if the operands have different types. === (strict equality) compares both values and types for equality without type coercion. Option 1 correctly describes this important distinction.
How does ESLint's --fix option handle issues that it can't automatically fix?
- ESLint raises an error and stops
- It logs the issues without attempting to fix them
- It automatically disables the rule causing the issue
- It leaves the issues as is
When ESLint's --fix option encounters issues it can't automatically fix, it will disable the rule causing the issue to prevent further errors. This allows ESLint to continue running and fix as many issues as possible without breaking. The other options do not accurately describe how --fix behaves.
In Pug, to extend a layout, you use the extends keyword and to fill a block within the layout, you use the ______ keyword.
- include
- fill
- block
- insert
In Pug, the extends keyword is used to extend a layout, and the block keyword is used to fill a block within the layout. This allows you to create modular and reusable templates. The other options do not serve the same purpose in Pug.
How does JavaScript's dynamic typing affect variable assignments and operations?
- It enforces strict type checking.
- It allows variables to change type during runtime.
- It requires explicit type annotations for all variables.
- It prevents type errors at compile time.
JavaScript's dynamic typing allows variables to change their data type during runtime. Unlike languages with static typing, you don't need to specify the data type of a variable explicitly.
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.
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.