You are working on a web application where you need to fetch data from an API, perform operations on it, and then use it to update the UI. Which JavaScript feature allows you to handle these asynchronous operations more readably and reliably?

  • Callback Functions
  • Promises
  • Event Loop
  • Callback Hell
Promises in JavaScript allow you to handle asynchronous operations more readably and reliably. Promises provide a structured way to work with asynchronous code, making it easier to manage tasks like fetching data from an API, performing operations, and updating the UI when the data is ready.

The keyword ______ is used to specify a block of code to be executed, if the same block that it is directly proceeding does not get executed.

  • break
  • continue
  • return
  • finally
The keyword finally is used to specify a block of code that will be executed regardless of whether an exception is thrown or not in a try...catch block. It ensures that cleanup code is always executed.

What will happen if the break statement is omitted in a switch case?

  • The program will continue to execute the following case(s) until a break or the end of the switch.
  • It will skip the current case and move on to the default case.
  • It will throw a syntax error.
  • It will automatically add a break statement, preventing fall-through.
If you omit the break statement in a switch case, JavaScript will execute the code for that case and continue to execute the code for the following case(s) until it encounters a break statement or reaches the end of the switch statement. This behavior is known as "fall-through."

The concept that allows JavaScript objects to inherit properties and behavior from an object of another class is known as _________.

  • inheritance
  • extension
  • encapsulation
  • polymorphism
The concept that allows JavaScript objects to inherit properties and behavior from an object of another class is known as "inheritance." Inheritance is a fundamental aspect of object-oriented programming and helps in code reusability.

To avoid iterating over prototype properties with for...in, you should use the _______ method.

  • Object.keys
  • hasOwnProperty
  • Object.prototype
  • Object.values
To avoid iterating over prototype properties with a for...in loop, you should use the Object.keys method. This method returns an array of an object's own enumerable property names, allowing you to iterate over only the object's properties without including those from its prototype chain.

The method getElementById selects an element using its ______.

  • ID
  • Tag Name
  • Class Name
  • Attribute
The method getElementById selects an element by its ID attribute. It returns the element with the specified ID attribute, providing a direct and efficient way to access an element in the DOM by its unique identifier.

A _________ function expression can be named, providing a reference to itself.

  • Recursive
  • IIFE (Immediately Invoked Function Expression)
  • Anonymous
  • Callback
A recursive function expression can be named, allowing it to refer to itself by name. This is useful in cases where a function needs to call itself during its execution, typically seen in solving recursive problems or implementing recursive algorithms.

You are designing a car simulation game using JavaScript. Each type of car (e.g., sedan, truck, etc.) has different methods for calculating fuel efficiency. Which object-oriented programming concept would be most appropriate to ensure that each car type can calculate fuel efficiency in its own way, while still inheriting basic characteristics from a general Car class?

  • Inheritance
  • Polymorphism
  • Encapsulation
  • Abstraction
Polymorphism allows different car types (e.g., sedan, truck) to have their own implementations of the fuel efficiency calculation method while inheriting common attributes and behaviors from the general Car class. This ensures flexibility and extensibility in your game.

You are developing a system where you have a base class "User" and two derived classes "Admin" and "Guest". If you want to add a method that is only applicable for "Admin" and not for "Guest", where should you add that method to adhere to the Liskov Substitution Principle?

  • In the "User" class
  • In the "Admin" class
  • In the "Guest" class
  • In a separate utility function
To adhere to the Liskov Substitution Principle, you should add the method specific to "Admin" in the "Admin" class. This ensures that each derived class (Admin and Guest) can be used interchangeably with the base class (User) without violating the principle.

Which keyword is used to check a condition in JavaScript?

  • for
  • if
  • while
  • switch
The keyword used to check a condition in JavaScript is if. It allows you to execute a block of code if a specified condition evaluates to true. The if statement is fundamental for controlling the flow of your JavaScript program.

How does JavaScript support lexical scoping?

  • By creating global variables
  • Through the use of the 'var' keyword
  • By nesting functions
  • By using block-level scoping with 'let'
JavaScript supports lexical scoping by allowing functions to access variables defined in their containing (enclosing) functions. This means that inner functions can "see" and use variables from outer functions, creating a chain of scope. This is achieved by nesting functions. Modern JavaScript also introduced 'let' for block-level scoping.

Which of the following scenarios is NOT recommended for using arrow functions?

  • As event handlers in the DOM.
  • As methods within objects.
  • In callbacks where "this" context matters.
  • In short, simple functions.
Arrow functions are not recommended for use as event handlers in the DOM. This is because the value of "this" in an arrow function is not determined by the event but instead retains the value from its enclosing lexical context, which may not be what you expect in an event handler. Traditional functions are usually preferred for event handlers.