Can ES6 class methods be used as constructors for creating new objects?

  • Yes, any class method can be used as a constructor
  • No, only the constructor method can be used as a constructor
  • Yes, but only static methods can be used as constructors
  • No, ES6 classes cannot be used to create new objects
In ES6 classes, only the constructor method can be used as a constructor to initialize new objects. Other methods, including static methods, are not used for object instantiation.

In a game, how would a static method be used to keep track of the total number of players created across all instances of the player class?

  • Use an instance method and increment a player counter in each instance.
  • Create a global variable to store the total number of players and update it in the player constructor.
  • Implement a static method that increments a static counter in the player class.
  • Utilize a separate counter class and delegate the counting task to it.
To keep track of the total number of players created across all instances in a game, a static method within the player class is suitable. This static method can maintain a static counter, ensuring it's shared among all instances and doesn't rely on the state of a specific player instance. Using a global variable may lead to scope-related issues and is less modular.

When developing an application with multiple dependent asynchronous API calls, would you choose Promises or callbacks? Explain why.

  • Use Promises
  • Use callbacks
  • It depends on the specific requirements
  • Combine Promises and callbacks
In modern JavaScript development, using Promises is a preferred choice for handling multiple dependent asynchronous API calls. Promises provide a cleaner and more readable syntax, making it easier to manage asynchronous code. Promises also allow for better error handling through the use of .catch() and can be easily chained, improving code maintainability.

Imagine a data processing module that requires several steps of data validation and transformation. How can functional composition and currying simplify this process?

  • Enhancing Readability and Reducing Code Duplication
  • Increasing Code Complexity and Maintenance
  • Decreasing Modularity and Reusability
  • Streamlining Error Handling and Debugging
Functional composition allows creating separate functions for each step of data processing, promoting modularity and reusability. Currying enhances this process by breaking down each step into smaller, composable functions. This results in a more readable, maintainable, and error-resistant data processing module. The combined use of functional composition and currying simplifies the entire data processing workflow and improves the overall efficiency of the system.

To pause and resume a generator function, the _________ keyword is used within the function body.

  • yield
  • await
  • pause
  • resume
In a generator function, the yield keyword is used to pause and resume the function's execution. It allows the generator to produce a sequence of values over time. The other options are not relevant to the purpose of pausing and resuming a generator.

Which of the following is a built-in higher-order function in JavaScript?

  • map()
  • add()
  • concat()
  • subtract()
The map() function in JavaScript is a built-in higher-order function that is commonly used to transform elements of an array. It takes a function as an argument and applies it to each element of the array.

In a scenario where a function calculates the total price of items in a cart, how would making this function pure affect its implementation?

  • It would introduce side effects
  • It would improve testability and predictability
  • It would slow down the function execution
  • It would require additional dependencies
Making the function pure in this scenario would involve removing external dependencies and ensuring that the function's output depends only on its input. This enhances testability and predictability, making the function easier to reason about and test in isolation. It also helps in avoiding side effects, which is a key characteristic of pure functions.

Can you reassign a new array or object to a variable declared with const?

  • Yes, always
  • Yes, but with restrictions
  • No, it will result in an error
  • Only if the variable is declared as a global constant
Variables declared with const cannot be reassigned to a new reference, whether it's an array, object, or any other data type. However, it's crucial to understand that this rule applies only to the reference itself, not the content of the variable. The content, if mutable (e.g., an object's properties), can still be modified.

What is the primary benefit of using Promises in AJAX calls over traditional callback functions?

  • Asynchronous handling
  • Synchronous handling
  • Simplicity
  • Compatibility
Promises provide a cleaner and more readable way to handle asynchronous operations, making code more maintainable and avoiding callback hell. They allow better error handling and chaining of multiple asynchronous operations.

What syntax is used to handle the promise returned by a dynamic import?

  • await import('module')
  • import('module').then()
  • dynamicImport('module')
  • import('module').catch()
The correct syntax to handle the promise returned by a dynamic import is import('module').then(). You use the then method to perform actions once the module has been successfully loaded. The catch method can be used for error handling if the dynamic import fails.