When using a do-while loop, the loop will always execute at least ________ time(s).

  • 0
  • 1
  • 2
  • 3
When using a do-while loop, the loop will always execute at least '1' time. This is because the loop condition is checked after the loop body has executed, so it will execute at least once before checking the condition for subsequent iterations.

When defining a method in an object, the function associated with a property is referred to as a _________.

  • variable
  • constructor
  • function
  • method
When defining a method in an object, the function associated with a property is referred to as a function. Methods are functions that are properties of objects. They allow you to perform actions or calculations using the data and context within the object. Understanding methods is fundamental in object-oriented programming with JavaScript.

In a UI with nested dropdown menus, a developer wants to ensure that clicking a nested menu item does not trigger the click event of its parent menu. What method can be used to stop the event from reaching the parent menu?

  • event.stopPropagation()
  • event.preventDefault()
  • event.stopImmediatePropagation()
  • event.cancelBubble()
To prevent the event from reaching the parent menu when clicking a nested menu item, you can use the event.stopPropagation() method. This will stop the event from propagating up the DOM tree and prevent the parent menu's click event from being triggered. event.preventDefault() is used to prevent the default behavior of an event, not to stop event propagation.

What is the main difference between function declaration and function expression in JavaScript?

  • Function declarations are hoisted, while function expressions are not.
  • Function expressions can be named or anonymous, while function declarations must have a name.
  • Function declarations are used for defining methods in objects, while function expressions are used for standalone functions.
  • Function expressions are more efficient than function declarations.
The primary difference between function declaration and function expression in JavaScript is hoisting. Function declarations are hoisted, which means they are moved to the top of their containing scope during compilation. This allows you to call the function before it's declared in your code. Function expressions, on the other hand, are not hoisted, so they can only be used after their declaration in the code. Understanding this difference is crucial for managing the order of function calls in your JavaScript programs.

What issues might arise due to JavaScript's prototype chain, and how might they be mitigated?

  • Issues may include unintentional property overwrites, inefficiency due to long chains, and unexpected inheritance. Mitigation involves using techniques like Object.create(), encapsulation, and avoiding global scope pollution.
  • Issues include limited encapsulation, increased memory usage, and reduced performance. Mitigation is achieved through using classes, constructors, and the ES6 "super" keyword for proper inheritance.
  • Problems include circular references, inability to hide properties, and difficulties with class-based modeling. Mitigation is achieved by avoiding circular references and using ES6 classes.
  • Problems might involve conflicts between prototypes, slow property access, and limited flexibility. Mitigation requires optimizing property access and using mixins.
JavaScript's prototype chain can lead to issues like unintentional property overwrites, inefficiency, and unexpected inheritance. To mitigate these, developers can use techniques like Object.create() to create clean, isolated objects, encapsulation to hide properties, and avoid global scope pollution.

You're debugging a JavaScript application and notice that a function defined within an object method using an arrow function is not behaving as expected. The "this" keyword is not referring to the object. What could be the reason for this?

  • Arrow functions always bind "this" to the object
  • Arrow functions don't have their own "this"
  • Objects cannot contain arrow functions
  • The object's properties are incorrectly defined
Arrow functions in JavaScript do not have their own "this" context. Instead, they inherit the "this" value from their containing function or the global context. If an arrow function is used inside an object method, it will use the "this" from the surrounding scope, which might not be the object itself.

When using querySelectorAll, the returned object is a _______.

  • NodeList
  • HTMLCollection
  • Array
  • Element
When you use querySelectorAll, it returns a NodeList. A NodeList is a collection of DOM elements that match the specified selector. Unlike an HTMLCollection, a NodeList is not live, which means it won't change dynamically as the document does.

Which part of a "for" loop is executed only once, when the loop starts?

  • Initialization
  • Condition
  • Increment
  • Body of the loop
The part of a "for" loop that is executed only once, when the loop starts, is the initialization. It is where you define and initialize loop control variables. The condition is checked before each iteration, the increment is executed after each iteration, and the body of the loop contains the code that is executed repeatedly until the condition is false.

In order to make an object iterable with a for...of loop, you need to define its _______ method.

  • loop
  • forEach
  • Symbol.iterator
  • Object.iterable
To make an object iterable with a for...of loop, you need to define its Symbol.iterator method. This method should return an iterator object, which must have a next method that provides values one at a time. This enables you to use a for...of loop to iterate over the object's elements.

You're developing a game and you're using a two-dimensional array to represent a grid of game cells. How could you access the third cell in the second row of a grid defined as const grid = [[1,2,3], [4,5,6], [7,8,9]]?

  • grid[2][1]
  • grid[1][2]
  • grid[3][2]
  • grid[2][3]
In JavaScript, two-dimensional arrays are accessed using two pairs of square brackets. To access the third cell in the second row, you should use grid[1][2], where the first index (1) represents the second row, and the second index (2) represents the third cell within that row.