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.

A(n) _______ function returns a promise.

  • Synchronous
  • Callback
  • Async
  • Static
An Async function returns a promise implicitly. When you declare a function as async, it means the function will always return a promise, allowing you to use await inside it to handle asynchronous operations more cleanly.

Which of the following is NOT an argument passed to an event listener when it is invoked?

  • event
  • target
  • type
  • timestamp
When an event listener is invoked, it receives an event object containing information about the event. It also has access to the target (the element that triggered the event) and type (the type of event). However, timestamp is not an argument passed to an event listener. It can be obtained using event.timeStamp.

What is the purpose of the this keyword inside a constructor function?

  • Refers to the global object
  • Refers to the parent object
  • Refers to the current instance
  • Refers to the next sibling
The this keyword inside a constructor function refers to the current instance of the object being created. It allows you to access and assign properties and methods to the instance being constructed, making it unique for each object created with the constructor. It does not refer to the global or parent object.

Which method would you use to add a new property to an object after it has been created?

  • Object.add()
  • Object.append()
  • Object.assign()
  • Object.insert()
To add a new property to an existing object in JavaScript, you would typically use the Object.assign() method. This method allows you to merge properties from one or more source objects into a target object. It can be used to add or update properties in the target object.

When using the new keyword to create an object, what kind of function must you use?

  • Regular Function Declaration
  • Arrow Function
  • Constructor Function
  • Prototype Function
When using the new keyword to create an object, you must use a Constructor Function. Constructor functions are used to create and initialize objects. They are invoked with the new keyword and often set object properties and methods within the constructor. Arrow functions are not suitable because they don't have their own this context.

The switch statement in JavaScript uses _________ comparison to evaluate cases.

  • Strict (===)
  • Loose (==)
  • Greater (>)
  • Lesser (<)
The switch statement in JavaScript uses strict (===) comparison to evaluate cases. This means that not only the value but also the data type must match for a case to be executed. This ensures accuracy when comparing values in a switch statement.

Imagine you’re refactoring a piece of code that involves a series of callback functions into a format that uses async/await. What should you pay extra attention to regarding error handling when refactoring the code?

  • Ensure proper try/catch
  • Maintain callback structure
  • Avoid async/await
  • Ignore error handling entirely
When refactoring code to use async/await, it's crucial to ensure proper error handling. This means using try/catch blocks around await operations to catch and handle any exceptions that may occur during asynchronous operations. Neglecting error handling could lead to unhandled exceptions and unexpected behavior.