How can one emulate the functionality of a switch statement using objects and functions in JavaScript?

  • By creating an object where each property maps to a case label, and the corresponding values are functions to execute.
  • By using a for loop to iterate through cases and execute corresponding functions.
  • By using the "continue" keyword in a loop to simulate case behavior.
  • By creating an array of functions, where each function corresponds to a case label.
To emulate the functionality of a switch statement in JavaScript, you can create an object where each property represents a case label, and the corresponding property values are functions that perform the desired actions. You can then use the input value to access the appropriate function from the object, effectively simulating switch behavior.

The createElement method is used to create a new element in the _________.

  • browser
  • JavaScript
  • document
  • DOM
The createElement method in JavaScript is used to create a new HTML element within the Document Object Model (DOM). It allows you to dynamically create HTML elements that can be added to the web page or modified as needed.

Which method returns a promise that resolves with the result of parsing the body text as JSON?

  • .json() method
  • .text() method
  • .body() method
  • .parseJSON() method
The .json() method of a Response object returned by fetch parses the response body text as JSON and returns a Promise that resolves with the parsed data. This method is commonly used when making API requests to obtain structured data in JSON format. The .text() method returns the response body as plain text, while .body() does not exist, and .parseJSON() is not a standard method in JavaScript.

A callback function is passed as an ________ to other functions and is executed after its parent function has completed.

  • argument
  • object
  • attribute
  • expression
A callback function is passed as an "argument" to other functions in JavaScript. It is a function that is executed after its parent function has completed its execution. This allows for asynchronous operations and event handling in JavaScript.

In JavaScript, instances are typically created using the _________ keyword.

  • class
  • new
  • instance
  • create
In JavaScript, instances of objects are typically created using the new keyword. For example, you can create a new instance of a constructor function using new ConstructorName(). This keyword is essential for creating object instances.

How do you declare a two-dimensional array in JavaScript?

  • let arr = [[]];
  • let arr = [][[]];
  • let arr = [[], []];
  • let arr = [[],] [[]];
In JavaScript, a two-dimensional array is declared by creating an array where each element is another array. This creates a matrix-like structure. For example, let arr = [[], []]; declares a 2x2 two-dimensional array. The other options contain syntax errors or do not create a valid two-dimensional array.

The _________ property of the XMLHttpRequest object holds the status of the XMLHttpRequest.

  • statusText
  • response
  • status
  • readyState
The status property of the XMLHttpRequest object holds the HTTP status code of the response received from the server. It indicates whether the request was successful or encountered an error. statusText contains the status message associated with the status code. response contains the response data.

Which of the following is NOT a type of polymorphism supported by JavaScript?

  • Compile-time polymorphism
  • Runtime polymorphism
  • Ad-hoc polymorphism
  • Parametric polymorphism
JavaScript supports runtime polymorphism and ad-hoc polymorphism (also known as function overloading), allowing different behavior for functions with the same name but different parameters. Parametric polymorphism is associated with generics and is not a form of polymorphism in JavaScript.

Which method should be used to handle exceptions in an async/await function?

  • try...catch
  • if...else
  • switch...case
  • throw...catch
To handle exceptions in an async/await function, you should use the 'try...catch' statement. This allows you to wrap the awaited code in a 'try' block and catch any exceptions that may occur within the 'catch' block. Using 'try...catch' ensures that your program can gracefully handle errors and prevent them from crashing your application.

What does a Promise represent in JavaScript?

  • A synchronous operation
  • An asynchronous operation
  • A specific data type
  • A built-in JavaScript function
A Promise in JavaScript represents an asynchronous operation that may or may not be completed at a certain point in time. It's used to handle tasks like fetching data from a server or reading a file, which can take some time to complete. Promises provide a way to work with asynchronous code in a more structured and readable manner.

Which JavaScript method is used to bind a function to a specific object?

  • .apply()
  • .bind()
  • .call()
  • .attach()
The .bind() method in JavaScript is used to create a new function that, when called, has its this keyword set to a specific object, allowing you to bind a function to that object. This is especially useful for ensuring the correct context when passing functions as callbacks or event handlers.

You have a block of code that needs to be executed when multiple conditions are true. Which control structure should be used to optimize the code for readability and performance?

  • if statement
  • switch statement
  • for loop
  • ternary operator
In this scenario, using the "if" statement is the most appropriate choice. It allows you to evaluate multiple conditions sequentially and execute a block of code if all specified conditions are true. This enhances code readability and maintains good performance.