The _________ method of an XMLHttpRequest object is used to send the request to a server.
- request()
- open()
- send()
- fetch()
The correct method to send a request to a server using an XMLHttpRequest object is the send() method. It sends the request to the specified URL, initiating the request to the server. The open() method is used to set up the request, while fetch() is used with the Fetch API.
Which of the following options is a technique to manage callback hell in JavaScript?
- Promises
- Event emitters
- Error handling strategies
- Callback chaining
Promises are a technique in JavaScript to manage callback hell. They provide a more structured and readable way to handle asynchronous operations, allowing you to chain multiple asynchronous calls together, making the code cleaner and easier to understand.
What kind of problem might closures introduce in your code if not used properly?
- Memory Leaks
- Faster Execution
- Enhanced Security
- Code Optimization
Closures, if not used properly, can introduce memory leaks in your code. When inner functions retain references to variables in the outer function, those variables can't be garbage collected even after they are no longer needed. This can lead to increased memory usage and decreased performance. To avoid memory leaks, it's essential to be mindful of how closures are created and when they are released.
You have a block of code that needs to be executed when multiple conditions are true. Which control structure should be used to optimize the code for readability and performance?
- if statement
- switch statement
- for loop
- ternary operator
In this scenario, using the "if" statement is the most appropriate choice. It allows you to evaluate multiple conditions sequentially and execute a block of code if all specified conditions are true. This enhances code readability and maintains good performance.
Which JavaScript method is used to bind a function to a specific object?
- .apply()
- .bind()
- .call()
- .attach()
The .bind() method in JavaScript is used to create a new function that, when called, has its this keyword set to a specific object, allowing you to bind a function to that object. This is especially useful for ensuring the correct context when passing functions as callbacks or event handlers.
What does a Promise represent in JavaScript?
- A synchronous operation
- An asynchronous operation
- A specific data type
- A built-in JavaScript function
A Promise in JavaScript represents an asynchronous operation that may or may not be completed at a certain point in time. It's used to handle tasks like fetching data from a server or reading a file, which can take some time to complete. Promises provide a way to work with asynchronous code in a more structured and readable manner.
Which method should be used to handle exceptions in an async/await function?
- try...catch
- if...else
- switch...case
- throw...catch
To handle exceptions in an async/await function, you should use the 'try...catch' statement. This allows you to wrap the awaited code in a 'try' block and catch any exceptions that may occur within the 'catch' block. Using 'try...catch' ensures that your program can gracefully handle errors and prevent them from crashing your application.
Which of the following is NOT a type of polymorphism supported by JavaScript?
- Compile-time polymorphism
- Runtime polymorphism
- Ad-hoc polymorphism
- Parametric polymorphism
JavaScript supports runtime polymorphism and ad-hoc polymorphism (also known as function overloading), allowing different behavior for functions with the same name but different parameters. Parametric polymorphism is associated with generics and is not a form of polymorphism in JavaScript.
The _________ property of the XMLHttpRequest object holds the status of the XMLHttpRequest.
- statusText
- response
- status
- readyState
The status property of the XMLHttpRequest object holds the HTTP status code of the response received from the server. It indicates whether the request was successful or encountered an error. statusText contains the status message associated with the status code. response contains the response data.
In order to create a private variable in JavaScript, you might utilize a ________.
- Closure
- Prototype
- Constructor
- Module
In order to create a private variable in JavaScript, you might utilize a "Module." A module is a design pattern that allows you to encapsulate data and functions, providing a way to create private variables and methods. This helps in achieving data encapsulation and preventing unwanted external access.