In a scenario where dynamic property names are based on user input, how would ES6 object literals be utilized?

  • Utilize computed property names to dynamically assign properties
  • Utilize the Object.assign method for dynamic property assignment
  • Use Object.create for dynamic property generation
  • Implement a switch statement for dynamic property naming
ES6 object literals allow the use of computed property names, which is particularly beneficial in scenarios where dynamic property names are based on user input. Computed property names enable the creation of object properties dynamically based on expressions, providing flexibility and conciseness in handling user input.

Consider a function that fetches user data from an API. How can this function be refactored to adhere to the principles of pure functions?

  • Return the fetched data directly
  • Introduce a callback for data processing
  • Include a random delay in fetching data
  • Use global variables for storing the fetched data
To make the function pure, it should return the fetched data directly without relying on external state or global variables. Introducing a callback or using global variables can introduce side effects, which contradicts the principles of pure functions.

When using const to declare a variable, what type of assignment is mandatory?

  • Reassignment
  • Initialization
  • Increment
  • Declaration
When you declare a variable with const, you must initialize it with a value. const variables cannot be declared without an initial value, and they cannot be reassigned to a different value after initialization.

What is the outcome when attempting to instantiate a class without the 'new' keyword in ES6?

  • It results in a runtime error
  • It creates a new instance but without the class's constructor being called
  • It has no impact; both 'new' and non-'new' instantiation work the same way
  • It leads to a memory leak
If a class is instantiated without the 'new' keyword, it throws a runtime error. The 'new' keyword is crucial for invoking the constructor and properly creating an instance of the class.

Q3: If you encounter a performance issue in a web application due to extensive use of prototypes, what would be your approach to optimize it?

  • Utilize static methods
  • Minimize prototype chain depth
  • Convert prototypes to classes
  • Use Object.setPrototypeOf()
To optimize performance, reducing the depth of the prototype chain is crucial. This minimizes the number of lookups required during property access, enhancing overall efficiency. It's essential to carefully design the prototype hierarchy for better performance in large-scale applications.

How can default parameters be used to create polymorphic functions in JavaScript?

  • By defining multiple functions with the same name and different default parameters
  • Default parameters cannot be used for polymorphism
  • Utilizing default parameters for error handling
  • By using default parameters to define optional arguments
Default parameters allow you to create polymorphic functions by defining multiple functions with the same name but different default parameters. This way, the function can behave differently based on the provided arguments, providing a form of polymorphism.

Imagine you are creating a gaming application with different types of characters. How would you use inheritance in ES6 classes to implement shared behaviors?

  • Object Composition
  • Using Mixins
  • Extending Base Class
  • Function Constructors
In this scenario, you would extend a base class to create subclasses for each character type. Option A is about combining objects, Option B is about sharing functionality through mixins, and Option D is an older approach not commonly used in ES6.

Each call to the next() method on a generator returns an object with two properties: value and _________.

  • result
  • done
  • output
  • complete
The next() method on a generator returns an object with two properties: value (the yielded value) and done (a boolean indicating whether the generator has completed). The other options do not represent the properties returned by the next() method.

In functional composition, what is the result of combining two functions f and g into compose(f, g)?

  • f(g(x))
  • g(f(x))
  • f * g
  • f + g
Functional composition applies functions from right to left, so compose(f, g) means applying f after g. Therefore, the result is equivalent to f(g(x)).

What happens when a generator function encounters a yield keyword?

  • Pauses the function execution
  • Throws an error
  • Skips the yield keyword
  • Resumes execution from the last yield
When a generator function encounters a yield keyword, it pauses the execution of the function, preserving its state. The function can later be resumed from the last yield encountered, allowing for efficient iteration over a sequence of values.

When exporting a class or function as a default export, it's not necessary to use the ________ keyword.

  • export default
  • default
  • export
  • class
When exporting a class or function as a default export, you can simply use export default followed by the class or function declaration without explicitly using the function or class keywords.

Higher-order functions that alter the execution context of another function are often referred to as __________.

  • Decorators
  • Generators
  • Closures
  • Combinators
Decorators in JavaScript are higher-order functions that alter the behavior or execution context of another function. They are often used to add functionalities to functions or methods.