Which API allows you to make non-simple requests to another domain in JavaScript, considering the Same-Origin Policy?

  • XMLHttpRequest
  • JSONP (JSON with Padding)
  • CORS (Cross-Origin Resource Sharing)
  • WebSocket
CORS (Cross-Origin Resource Sharing) is the API that allows you to make non-simple requests (such as those with custom headers or methods) to another domain in JavaScript while considering the Same-Origin Policy. XMLHttpRequest is an older alternative but has limitations compared to CORS. JSONP is a workaround technique, not an API, and WebSocket is used for full-duplex communication, not for cross-origin requests.

The concept of creating a single function or method that can operate on multiple types of objects is known as _________.

  • Overloading
  • Inheritance
  • Polymorphism
  • Abstraction
The concept of creating a single function or method that can operate on multiple types of objects is known as "Polymorphism." In JavaScript, polymorphism can be achieved through method overriding and dynamic method binding, enabling different objects to respond to the same method in a way that suits their specific type.

How can you implement a while loop without causing a browser to freeze in case of a long-running process?

  • Use asynchronous code and setTimeout to break the loop into smaller chunks, allowing the browser to handle other tasks.
  • Wrap the loop in a Web Worker to offload it to a separate thread.
  • Increase the browser's JavaScript execution stack size to handle long-running loops.
  • Use a while loop with a higher timeout value to prevent freezing.
To prevent the browser from freezing during a long-running while loop, you should use asynchronous code and setTimeout to break the loop into smaller chunks. This allows the browser to handle other tasks and prevents the UI from becoming unresponsive. This approach follows the principles of non-blocking and responsive UI in web development.

What is the primary difference between while and do-while loops in JavaScript?

  • The while loop always executes the code block at least once
  • The do-while loop is more efficient
  • The while loop is not suitable for iterating over arrays
  • The do-while loop doesn't have a condition
The primary difference is that in a while loop, the condition is checked before executing the code block. If the condition is initially false, the code block may never run. In contrast, the do-while loop guarantees that the code block is executed at least once because it checks the condition after executing the block. This makes do-while useful when you want the code to run once before checking the condition.

The ________ operator can be used to compare both value and type in JavaScript.

  • Equality (===)
  • Assignment (=)
  • Identity (==)
  • Inequality (!=)
The Equality (===) operator can be used to compare both value and type in JavaScript. It checks if two values are strictly equal, meaning they have the same value and the same data type. The other options, such as Assignment (=) and Identity (==), do not perform strict value and type comparison. Inequality (!=) checks for inequality.

The process of an object inheriting properties and behaviors (methods) from its prototype is known as _________.

  • Encapsulation
  • Inheritance
  • Polymorphism
  • Abstraction
Inheritance is the process by which an object inherits properties and behaviors (methods) from its prototype or parent object. JavaScript uses a prototype-based inheritance model, where objects inherit from other objects, and this is a fundamental concept in the language.

The ________ method is used to read a Response stream and return it as a blob.

  • text()
  • json()
  • blob()
  • arrayBuffer()
The blob() method is used to read a Response stream and return it as a blob object. This is commonly used when dealing with binary data or files. The other options (text(), json(), arrayBuffer()) are used for different types of Response data processing.

You are debugging a web application and notice that a "for" loop is causing the webpage to hang. What could be a potential reason and how might you solve it?

  • Infinite Loop
  • Excessive DOM Manipulation
  • Missing Loop Condition
  • Insufficient Memory Allocation
One potential reason for a webpage hang caused by a "for" loop is excessive DOM manipulation within the loop, which can block the main thread. To solve this, consider moving DOM manipulation outside the loop or optimizing the DOM operations. An infinite loop, missing loop condition, or insufficient memory allocation could also cause hangs but are less likely in this context. JavaScript Debugging

What is lexical scoping in JavaScript?

  • A type of error handling in JavaScript
  • A scoping mechanism in JavaScript
  • A method to declare global variables
  • A JavaScript data type
Lexical scoping in JavaScript refers to the way variable scope is determined by the placement of variables in the source code. Variables declared inside a function are accessible within that function, creating a hierarchy of scope. This helps prevent variable conflicts and promotes clean code.

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.