How are escape sequences like n treated in template literals?

  • Treated as a newline character
  • Ignored
  • Treated as a literal backslash followed by 'n'
  • Throws an error
In template literals, escape sequences like n are treated as newline characters. This allows for multi-line strings without the need for explicit line break characters.

In what order are tasks executed given JavaScript’s event loop and call stack mechanism?

  • Based on the complexity of the task
  • First In, First Out (FIFO) order
  • Random order
  • Last In, First Out (LIFO) order
Tasks in JavaScript are executed in a First In, First Out (FIFO) order. The event loop continually checks the call stack and executes tasks in the queue. This order ensures that tasks are processed in the sequence they are received, maintaining the integrity of the program's logic.

In a scenario where you need to convert an array of user objects into an array of names, which array method is the most suitable?

  • map
  • filter
  • reduce
  • forEach
In this scenario, the most suitable array method is map. The map method is designed for transforming each element of an array and returning a new array with the transformed values. It allows you to extract the names from the user objects efficiently.

In a scenario where you need to create private-like properties in an object, how would the Symbol type be used effectively?

  • Using Symbol.createPrivateProperty
  • Using Symbols as property names
  • Using Object.defineProperty with Symbol
  • Using private keyword
The correct option is using Symbols as property names. Symbols are unique and can be used as keys for object properties, providing a way to create private-like properties in an object. They help in avoiding naming conflicts and provide a level of encapsulation. Using Object.defineProperty with Symbol is a common approach to achieve this. The other options are incorrect as there is no direct Symbol.createPrivateProperty method, and the private keyword is not a standard feature in ES6 for creating private properties.

Prototype-based inheritance in JavaScript is an example of _________ inheritance, as opposed to class-based inheritance.

  • Single
  • Multiple
  • Hierarchical
  • Object
JavaScript uses prototype-based inheritance, where objects can inherit properties from other objects, forming a hierarchical chain of prototypes. This is different from class-based inheritance systems.

Q3: Consider an application that monitors the status of DOM elements. How would a WeakSet be beneficial in this context?

  • WeakSet
  • Set
  • Both WeakSet and Set
  • Neither WeakSet nor Set
In the context of monitoring DOM element status, a WeakSet is beneficial. This is because a WeakSet can hold references to DOM elements without preventing them from being garbage collected when they are removed from the DOM. As DOM elements are added and removed dynamically, a WeakSet ensures that the references do not cause memory leaks. If a DOM element is removed from the document, its reference in the WeakSet becomes automatically eligible for garbage collection. A Set, on the other hand, would keep references even if the DOM elements are removed, potentially leading to memory issues in the long run.

Mixins in ES6 are commonly used for adding _________ to classes without using inheritance.

  • Properties
  • Methods
  • Behavior
  • Variables
Mixins in ES6 are commonly used for adding behavior (methods) to classes without using traditional inheritance. This approach enhances code flexibility and avoids the potential downsides of deep class hierarchies.

What is the primary purpose of the spread operator in ES6?

  • Spreading elements of an array or object
  • Merging two arrays
  • Concatenating strings
  • Creating a new variable
The spread operator (...) is primarily used to spread elements of an array or object, allowing them to be expanded in places where multiple elements or variables are expected. This can be useful for passing array elements as individual arguments to a function or merging arrays.

How do default parameters affect the arguments object in a function?

  • They override the arguments object
  • They have no impact on the arguments object
  • They are added to the arguments object
  • They remove the arguments object
Default parameters in ES6 are added to the arguments object, making it a useful feature for accessing all passed arguments, even if they are not explicitly named in the function signature. This can be advantageous in certain scenarios where a dynamic number of arguments need to be handled.

Does a function that reads external files qualify as a pure function?

  • Yes, as long as it only reads and does not modify
  • No, any file reading operation is impure
  • Only if it's a text file
  • Only if it's a small file
No, reading external files is considered a side effect, and pure functions should not have side effects. Pure functions should only operate on their input parameters and not rely on external factors such as file systems or databases.