A _________ object is used to perform HTTP requests in AJAX.

  • XMLHttpRequest
  • JSON
  • DOM
  • Fetch
In AJAX (Asynchronous JavaScript and XML), the XMLHttpRequest object is used to perform HTTP requests asynchronously. It allows you to send and receive data from a server without refreshing the entire web page.

Which property of the event object is commonly used to prevent the default action of the event?

  • event.stopPropagation()
  • event.preventDefault()
  • event.cancelBubble()
  • event.halt()
To prevent the default action of an event in JavaScript, you commonly use the event.preventDefault() method. It stops the default behavior associated with the event, such as preventing a form from submitting or a link from navigating to a new page. This method is crucial for controlling the behavior of events.

You have an object containing user data and need to create an array of strings containing user details in a "key: value" format. Which loop might be most suitable for this task?

  • for...in loop
  • for...of loop
  • while loop
  • forEach() method
The for...of loop is most suitable for iterating over object properties when you want to create an array of strings. It directly iterates over iterable values like arrays and works well for this task by extracting key-value pairs from the object.

Which of the following is NOT a characteristic of closures in JavaScript?

  • Data encapsulation and privacy
  • Persistence of local variables
  • Ability to access outer variables
  • Limited use of memory resources
Closures in JavaScript are powerful because they allow functions to remember and access their outer (enclosing) variables even after the outer function has finished executing. This feature can lead to memory usage if not managed properly, but it's not a limitation or disadvantage of closures per se.

The ______ statement is used to specify a new condition to test if the first condition is false.

  • else if
  • else
  • if else
  • switch
The else if statement is used to specify a new condition to test if the first condition in an if statement is false. It allows for branching in code execution based on multiple conditions. It's a fundamental control structure in JavaScript.

You're debugging a piece of code that is returning an array in an unexpected order after a sort() method is applied. What could be a likely cause for this behavior given the default behavior of sort()?

  • The array has mixed data types
  • The sort() function is asynchronous
  • The array elements are all numbers
  • The array elements are strings
JavaScript's sort() method by default converts elements to strings and then compares their UTF-16 code units. This means that if the array contains mixed data types, the sorting order might be unexpected. For proper sorting, you should provide a compare function as an argument to sort().

A piece of code is unexpectedly returning -Infinity. What arithmetic operation could be causing this?

  • Division (/)
  • Addition (+)
  • Exponentiation (**)
  • Multiplication (*)
When a piece of code returns -Infinity, it is usually due to a division operation where you are dividing a finite number by zero. In JavaScript, dividing any finite number by zero results in -Infinity. Make sure to handle cases where division by zero can occur to prevent unexpected -Infinity values in your code.

Consider a situation where you have a switch statement inside a function, and forgetting to include a break statement leads to a bug. How might this bug manifest in the function’s behavior?

  • The function might return the value associated with the first matching case, and all subsequent code within the switch block will execute as well.
  • The function will throw an error, indicating a missing "break" statement, and won't execute any code within the switch block.
  • The function will automatically insert "break" statements at the end of each case, ensuring correct behavior.
  • The function will ignore the switch statement and continue executing the code outside of the switch block.
If you forget to include a "break" statement in a switch case, it will lead to a bug where the function may not behave as expected. Instead of stopping after the first matching case, the switch statement will "fall through" to subsequent cases, causing unintended behavior. The correct option is to use a "break" statement to exit the switch block after handling a case. JavaScript doesn't automatically insert "break" statements, and it doesn't throw an error for missing "break" statements.

The concept of block scope is introduced in ECMAScript 6 with the new keywords _________ and const.

  • let and var
  • block and const
  • scope and let
  • const and var
Block scope in JavaScript is introduced using the let keyword, which allows variables to be declared with block-level scope, while const is used to declare constants. The var keyword does not provide block scope and was used in older versions of JavaScript.

You are developing an application that continuously checks for incoming messages and processes them immediately. Which looping structure could be used to handle message checking and processing, and what considerations should be taken into account for performance and user experience?

  • for...of loop with asynchronous functions
  • setInterval with an event-driven approach
  • do-while loop with synchronous functions
  • setTimeout with an event-driven approach and callbacks
Using setInterval with an event-driven approach is a suitable choice for continuously checking and processing incoming messages. This approach allows you to define a specific interval for checking messages, balancing performance and user experience. Options like for...of with asynchronous functions might not provide consistent timing, and do-while with synchronous functions could lead to performance issues. setTimeout with callbacks is more suitable for one-time delays.