In JavaScript, a variable declared without the var, let, or const keyword inside a function becomes a property of the ________ object.

  • "window"
  • "global"
  • "local"
  • "this"
In JavaScript, a variable declared without the var, let, or const keyword inside a function becomes a property of the "global" object. This can lead to unexpected behavior, so it's important to declare variables properly.

In which scenario would denormalization be considered a suitable option for query optimization?

  • When you want to minimize data redundancy and improve data integrity.
  • When you want to improve query performance, even at the cost of some data redundancy and update anomalies.
  • Denormalization is never considered a suitable option for query optimization.
  • When you want to reduce the size of the database.
Denormalization is considered when you want to optimize read-heavy queries and reduce the number of JOIN operations. It involves introducing redundancy to improve query performance, but it can lead to data anomalies.

How can you simulate user actions like clicks or keyboard inputs in Jest?

  • jest.spyOn()
  • jest.mock()
  • jest.fn()
  • jest.simulate()
In Jest, you can simulate user actions like clicks or keyboard inputs using jest.fn(). This allows you to create mock functions that can simulate user interactions and track their calls. The other options have different purposes; jest.spyOn() is used to spy on method calls, jest.mock() is used to mock modules, and jest.simulate() is not a valid Jest method for simulating user actions.

How can you create a custom lifecycle event that runs a series of npm scripts in a specified order?

  • Use the pre and post prefixes with custom script names
  • Use a third-party package like "npm-run-all"
  • It's not possible to create custom lifecycle events
  • Use JavaScript code within package.json
You can create a custom lifecycle event that runs a series of npm scripts in a specified order by using a third-party package like "npm-run-all." This package allows you to define complex run scripts in a convenient way, specifying the order of execution and handling dependencies between scripts.

You are working on a large codebase with multiple developers, and you notice inconsistencies in coding styles. How can ESLint help in maintaining a consistent coding style across the project?

  • Manually review and correct code style issues.
  • Create a shared ESLint configuration and enforce it across the project.
  • Ignore coding style issues to avoid conflicts.
  • Encourage developers to use their preferred coding styles.
ESLint can help maintain a consistent coding style by creating a shared ESLint configuration that defines the coding style rules. This configuration can be enforced across the project, ensuring that all developers adhere to the same coding standards. Manually reviewing, ignoring issues, or allowing personal preferences would lead to inconsistencies.

You are building an Express.js API and need to ensure that the API can only be accessed with a valid authentication token. How would you implement middleware to secure your API?

  • Use a middleware function to check the authentication token for each API route and grant access only if the token is valid.
  • Implement authentication within each route handler, verifying the token before processing the request.
  • Rely on HTTPS encryption to secure the API and avoid using authentication middleware.
  • Use a third-party authentication service to secure your API and handle token validation externally.
To secure an Express.js API with authentication, you should create a middleware function that checks the authentication token for each API route and grants access only if the token is valid. Centralizing authentication in middleware ensures consistent security across all routes. The other options are either less secure or less maintainable.

You are responsible for the security of a web application. You have to ensure that only trusted domains can interact with your server. How would you configure CORS to allow only specific domains to make requests to your server?

  • Set the Access-Control-Allow-Origin header to * in the server response.
  • Use wildcard subdomains in the Access-Control-Allow-Origin header.
  • Specify the trusted domains in the Access-Control-Allow-Origin header.
  • Disable CORS entirely to prevent any external access.
To restrict CORS to specific domains, you should specify the trusted domains in the Access-Control-Allow-Origin header. Option A would allow any domain, option B uses wildcard subdomains, which may not be secure, and option D is not secure and disables CORS.

Which of the following is used to consume a Promise?

  • then()
  • catch()
  • finally()
  • consume()
To consume the result of a Promise, you typically use the then() method. It allows you to specify what should happen when the Promise is successfully resolved. The other options are also Promise methods but serve different purposes, like error handling with catch() or running code regardless of the Promise's outcome with finally().

What is the primary purpose of using JSON Web Tokens (JWT) in authentication?

  • Storing user passwords securely
  • Encoding user data in URL parameters
  • Storing user sessions on the server
  • Securely transmitting information between parties
The primary purpose of using JSON Web Tokens (JWT) in authentication is to securely transmit information between parties. JWTs are used to authenticate users and ensure that data exchanged between the client and server remains tamper-proof and confidential. They are commonly used in web applications to maintain user sessions without the need to store session data on the server. The other options do not represent the primary purpose of JWTs.

In Express, how can you enable Cross-Origin Resource Sharing (CORS) for your API?

  • app.use(cors())
  • app.enable(cors)
  • app.allow(CORS)
  • app.cors(true)
To enable Cross-Origin Resource Sharing (CORS) in Express, you should use the cors middleware by adding app.use(cors()) to your application. This middleware allows or restricts cross-origin HTTP requests. The other options are not valid ways to enable CORS in Express.

How can you create an object in JavaScript that does not inherit the prototype from Object?

  • Object.create(null)
  • Object() constructor
  • {} constructor
  • Object.assign({})
In JavaScript, you can create an object that does not inherit the prototype from Object by using Object.create(null). This creates an object with no prototype, making it a clean slate. The other options either inherit from Object or use Object's prototype.

How can the 'done' callback be used in asynchronous testing with Mocha?

  • done is used to terminate the Mocha test suite prematurely.
  • done is used to indicate that the test is asynchronous and should wait for it to complete.
  • done is used to skip a test case in Mocha.
  • done is not used in Mocha for asynchronous testing.
In Mocha, the done callback is used to indicate that a test is asynchronous and should wait for it to complete before considering the test case finished. It prevents the test case from finishing prematurely. The other options provide incorrect information about the use of done.