How do you declare a two-dimensional array in JavaScript?
- let arr = [[]];
- let arr = [][[]];
- let arr = [[], []];
- let arr = [[],] [[]];
In JavaScript, a two-dimensional array is declared by creating an array where each element is another array. This creates a matrix-like structure. For example, let arr = [[], []]; declares a 2x2 two-dimensional array. The other options contain syntax errors or do not create a valid two-dimensional array.
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.
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.
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.
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 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.
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.
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.
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.
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.
What does the pop() method do to a JavaScript array?
- Removes the last element
- Removes the first element
- Adds an element to the end
- Adds an element to the beginning
The pop() method in JavaScript removes the last element from an array and returns that element. This operation reduces the length of the array by 1. It's the opposite of the push() method, which adds an element to the end of the array. pop() is useful for removing elements from the end of a stack-like structure.
The method _______ returns the index of the first element in the array that satisfies the provided testing function.
- findIndex()
- indexOf()
- search()
- locateIndex()
The findIndex() method is used to find the index of the first element in an array that satisfies a provided testing function. It iterates through the array, executing the function for each element until a match is found or it reaches the end of the array.