Which method is preferred to loop through an array in ES6 and onwards?
- for loop
- for...in loop
- forEach method
- while loop
In ES6 and onwards, the preferred method to loop through an array is the forEach method. It provides a concise way to iterate over array elements and execute a function for each element. The for loop and for...in loop are less commonly used for iterating arrays, and the while loop is a general-purpose loop that can be used but is not specifically designed for array iteration.
The main difference between a function expression and a function declaration is the function _________.
- Hoisting
- Scope
- Expression
- Name
The main difference between a function expression and a function declaration is the function name. Function declarations have a name, which is used to call the function, while function expressions may or may not have a name, depending on whether they are named or anonymous functions.
Which arithmetic operator is used to calculate the remainder in JavaScript?
- % (Modulus)
- / (Division)
- * (Multiplication)
- + (Addition)
In JavaScript, the % (Modulus) operator is used to calculate the remainder of a division operation. For example, 7 % 3 equals 1 because when 7 is divided by 3, the remainder is 1.
In Java, a variable declared within a method is referred to as a ________ variable.
- Global
- Instance
- Local
- Static
In Java, a variable declared within a method is referred to as a "Local" variable. Local variables are defined within a method and are only accessible within that method's scope. They are used for temporary storage and are typically used to store intermediate results or data specific to a method.
The ________ method in the Stream API returns an equivalent stream that is sequential.
- filter
- map
- parallel
- sequential
In the Stream API of Java, the sequential() method is used to return an equivalent stream that is sequential in nature. This method can be useful when you want to ensure that subsequent operations on the stream are executed in a sequential order.
What is the primary difference between Runnable and Callable interfaces?
- Callable can be scheduled for future execution.
- Callable can run without being wrapped in a thread.
- Runnable allows returning a result.
- Runnable can be used for multi-threading.
The primary difference between Runnable and Callable interfaces is that Callable allows you to return a result from the computation, whereas Runnable does not. Callable is typically used when you need a result from a task that can be scheduled for future execution, while Runnable is a simple interface for a task that does not return a result.
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 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.
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.
How does hoisting behave in function declarations in JavaScript?
- Function declarations are moved to the top of their containing scope during compilation.
- Function declarations are not affected by hoisting.
- Hoisting only applies to variables, not functions.
- Function declarations are moved to the bottom of the code.
In JavaScript, hoisting is the mechanism by which variable and function declarations are moved to the top of their containing scope during compilation. This means that you can call a function declared with function before it appears in your code, and it will still work. However, it's important to note that only the declarations are hoisted, not the initializations. Understanding hoisting is crucial for writing clean and maintainable JavaScript code.
How can you add a method to an object in JavaScript?
- a) By using the Object.addMethod() method.
- b) By defining a function and assigning it as a property of the object.
- c) By using the Object.method() function.
- d) By using the object.method = function() syntax.
You can add a method to an object in JavaScript by defining a function and assigning it as a property of the object. For example, myObject.myMethod = function() { /* method code */ };. While you can use various patterns and techniques for method definition, there's no standard Object.addMethod() or Object.method() function.
When the interpreter encounters the following code var x = "5"; the typeof x will be _________.
- "string"
- "number"
- "boolean"
- "undefined"
In JavaScript, the typeof operator is used to determine the data type of a variable. When var x = "5"; is encountered, the value of x is a string because it is enclosed in double quotes. Therefore, typeof x will return "string". It's important to note that JavaScript is dynamically typed, meaning the type of a variable can change during runtime.