Which design principle is violated if a superclass is aware of its subclasses?

  • Encapsulation
  • Inheritance
  • Polymorphism
  • Abstraction
When a superclass is aware of its subclasses, it violates the principle of encapsulation. Encapsulation refers to the concept of bundling data and the methods that operate on that data into a single unit or class. In object-oriented programming, classes should not have direct knowledge of their subclasses to promote loose coupling and maintainability. Instead, subclasses should inherit behavior and attributes from the superclass without the superclass needing to know specific details about its subclasses. Violating this principle can lead to code that is less flexible and harder to maintain.

Which method is commonly used to change the text content of an HTML element using JavaScript?

  • innerHTML
  • setAttribute
  • appendChild
  • createTextNode
The innerHTML property is commonly used to change the text content of an HTML element using JavaScript. It allows you to set the HTML content of an element, including its text. For example, you can use element.innerHTML = "New Text" to change the content of an element.

What is the primary purpose of classes in JavaScript?

  • Defining constants
  • Object creation
  • Error handling
  • Loop iteration
The primary purpose of classes in JavaScript is to facilitate object creation and define blueprints for objects. Classes act as templates for creating objects with shared properties and methods, enhancing code organization and reusability. While JavaScript does have constants, error handling, and loops, these are not the primary purposes of classes.

If an array arr has a length n, executing arr.push(x) will set arr[_______] equal to x.

  • arr.length - 1
  • arr.length + 1
  • arr.length
  • arr.length - 2
When you use arr.push(x), it appends the element x to the end of the array and increases the length property of the array by 1. Therefore, the new element will be at the index arr.length - 1, which is one less than the new length. For example, if arr has a length of 5, arr.push(x) will add x at index 4.

You are developing a game using JavaScript. Players continue to the next level as long as their score is below a certain threshold. Which looping structure might be the most appropriate to check player scores and why?

  • for loop
  • while loop
  • do-while loop
  • if-else statement with recursion
A while loop is the most suitable option for this scenario. It allows continuous checking of the player's score without the need for a fixed number of iterations. The condition in the while loop can be based on the score threshold, ensuring players progress to the next level when their score is below the threshold. The other options do not provide a continuous loop structure required for this task.

The _______ keyword is used to define a constructor inside a class to create objects.

  • this
  • super
  • constructor
  • new
In JavaScript classes, the constructor keyword is used to define the constructor method. This constructor method is automatically called when you create a new object from the class. It is used to initialize the object's properties and perform any setup required.

How can one implement a switch statement to handle multiple data types efficiently?

  • By using the "case" keyword for each data type and applying type conversion in each case.
  • By using the "default" keyword to handle unexpected data types.
  • By wrapping the switch statement in a try-catch block for error handling.
  • By using an array of functions, where each function handles a specific data type.
To handle multiple data types efficiently in a switch statement, you can create an array of functions, where each function corresponds to a specific data type. When you receive input, you can then call the appropriate function based on the data type, which offers more flexibility and avoids type conversion complexities.

You've encountered a bug in your JavaScript code where a variable is not retaining an expected value within a nested function. What feature of JavaScript should you explore to troubleshoot this issue?

  • Lexical Scoping
  • Hoisting
  • IIFE (Immediately Invoked Function Expression)
  • Event Delegation
To troubleshoot the issue of a variable not retaining an expected value within a nested function, you should explore the concept of lexical scoping. Lexical scoping (also known as static scoping) means that the scope of a variable is determined by its location within the source code, and nested functions have access to variables from their containing (parent) functions. This understanding will help you identify and fix the problem.

In a performance-critical application, minimizing the reshuffling of array elements is vital. What method might be avoided when removing elements due to its time complexity?

  • splice()
  • push()
  • pop()
  • shift()
The splice() method should be avoided when removing elements from an array in a performance-critical application because it has a time complexity of O(n) due to the need to shift elements after the removal. Methods like pop() (O(1)), shift() (O(1)), or slice() (O(n)) may be more appropriate.

In a while loop, forgetting to update the condition can lead to a/an ________ loop.

  • Infinite
  • Endless
  • Eternal
  • Unstoppable
In a while loop, if you forget to update the condition, it can lead to an "infinite" loop, where the loop continues to execute indefinitely. This can cause the program to hang or become unresponsive. It's important to ensure that the loop's condition is updated to eventually become false.