If you’re using arrow functions to define methods inside a class, those methods will not have access to the class’s instance without using _________.

  • super()
  • this
  • prototype
  • new
If you use arrow functions to define methods within a class, they will not have their own this context. Instead, they inherit the this context from the surrounding scope. To access the class instance within an arrow function, you need to use this. Hence, the correct option is this.

In Internet Explorer, instead of addEventListener, the _________ method is used to attach event listeners.

  • attachEvent()
  • registerListener()
  • listenForEvent()
  • addListener()
In Internet Explorer, the attachEvent() method is used to attach event listeners. This method is specific to Internet Explorer and serves a similar purpose to addEventListener() in other browsers, allowing you to respond to events such as clicks and keypresses.

In JavaScript, a common way to utilize prototypes is by assigning a(n) _________ to an object's prototype property.

  • Array
  • Object Literal
  • Constructor Function
  • Prototype Object
In JavaScript, prototypes are often utilized by assigning a constructor function to an object's prototype property. This constructor function serves as a blueprint for creating objects with shared properties and methods. The prototype object associated with this constructor contains those shared properties and methods.

You are developing a function that needs to maintain state between calls without using global variables. Which JavaScript feature would you utilize to achieve this?

  • Closures
  • Prototypes
  • Callbacks
  • Promises
You can utilize closures in JavaScript to maintain state between function calls without relying on global variables. A closure is a function that has access to variables from its outer (enclosing) function's scope, even after the outer function has finished executing. This allows you to create private variables and maintain state within the closure function.

In a do-while loop, when is the condition checked?

  • Before executing the code block
  • After executing the code block
  • During the execution of the code block
  • It's not checked in a do-while loop
In a do-while loop, the condition is checked after executing the code block. This means that the code block will always execute at least once before the condition is evaluated. If the condition is true after the first execution, the loop will continue running; otherwise, it will exit. This makes do-while loops suitable when you want to ensure that a specific task is performed before checking the condition.

How does event looping handle while(true) in Node.js environments?

  • It blocks the event loop indefinitely, causing the application to hang.
  • It executes the loop in a separate thread to avoid blocking.
  • It sets a maximum execution time for the loop to prevent hanging.
  • It queues the while(true) in the event loop, allowing other events to execute.
In Node.js, using while(true) will block the event loop indefinitely, causing the application to hang. This is because Node.js is single-threaded and relies on an event loop to handle asynchronous tasks. Long-running synchronous code like while(true) can prevent other tasks from being executed.

In a scenario where you have to create a function that acts differently based on the type of input (string, number, or boolean), how might you implement this using a switch statement?

  • Use separate case statements for each data type, and within each case, handle the specific behavior for that data type.
  • Convert the input to a string, and then use a single case statement for "string", "number", and "boolean" as switch cases.
  • Create individual functions for each data type and call the appropriate function based on the input type.
  • Use an if-else ladder instead of a switch statement to handle different data types.
When implementing a function that acts differently based on input types using a switch statement, it's best practice to use separate case statements for each data type. This ensures clarity and maintainability of your code, as each case can handle the specific behavior for that data type. Using a single case statement with type conversion is less readable and may lead to unexpected behavior. Individual functions for each type would increase code complexity unnecessarily, and using an if-else ladder is less efficient and less idiomatic in JavaScript.

Which method would you use to replace an HTML element with another?

  • replaceElement()
  • replaceChild()
  • swapElement()
  • switchElement()
The correct method to replace an HTML element with another in JavaScript is replaceChild(). This method allows you to replace a child element within a parent element. It takes two arguments: the new element to be inserted and the old element to be replaced.

The ______ statement is used to specify a block of code to be executed if the condition is false.

  • if-else
  • else-if
  • if
  • switch
The "else" statement is used to specify a block of code to be executed if the condition specified in the preceding "if" statement is false. It allows for conditional execution, and if the "if" condition is not met, the code inside "else" will be executed.

How can a subclass be created in JavaScript?

  • Using the extends keyword
  • By defining a new class with the same name
  • Using the super keyword
  • By using the inherit keyword
In JavaScript, you can create a subclass by using the extends keyword. This allows you to inherit the properties and methods of another class, forming a class hierarchy. The extends keyword establishes a relationship between the subclass and the superclass.

What is the primary use of the switch statement in JavaScript?

  • To declare variables
  • To create loops
  • To perform conditional branching
  • To define functions
The primary use of the switch statement in JavaScript is to perform conditional branching. It allows you to execute different code blocks based on the value of an expression, making it a powerful tool for decision-making in your code.

The ________ method is used to parse a JSON string into a JavaScript object.

  • toJSON()
  • parseJSON()
  • JSON.parse()
  • stringToJSON()
The correct method to parse a JSON string into a JavaScript object is JSON.parse(). It takes a JSON string as input and returns a JavaScript object. The other options (toJSON(), parseJSON(), stringToJSON()) are not valid JSON parsing methods.