What is "Promise chaining" in JavaScript?
- A method for nesting multiple if statements
- A technique for connecting multiple Promises
- A way to synchronize asynchronous functions
- A method to define variables
"Promise chaining" is a technique in JavaScript where you can connect multiple Promises together. It allows you to execute asynchronous operations sequentially, making code more readable and manageable, especially when dealing with multiple async tasks.
How does the prototype property behave in arrow functions?
- Arrow functions have a prototype property.
- Arrow functions do not have a prototype.
- Arrow functions inherit their prototype.
- Arrow functions override their prototype.
Arrow functions do not have their own this context and do not have a prototype property. Unlike regular functions, arrow functions do not bind their own this value or have a prototype property. This is a key difference to keep in mind when choosing between regular functions and arrow functions in JavaScript.
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 method myArray.find(callback) returns _______ if no element passes the test.
- NaN
- undefined
- an empty array
- -1
The myArray.find(callback) method returns undefined if no element in the array passes the test provided by the callback function. This is because it signifies that no element satisfies the condition. If an element is found that passes the test, it returns that element.
Which of the following is NOT a state of a Promise?
- Pending
- Resolved
- Rejected
- Completed
In JavaScript, a Promise can be in one of three states: Pending (initial state), Resolved (fulfilled with a value), or Rejected (fulfilled with an error). "Completed" is not a valid state for a Promise; it's either resolved or rejected.
What is the impact on performance when using a switch statement versus multiple if-else statements for numerous conditions?
- Switch statements are generally faster than multiple if-else statements for numerous conditions because they use direct lookup tables.
- Switch statements are slower than multiple if-else statements for numerous conditions due to increased code complexity.
- There is no significant difference in performance between switch and if-else statements for numerous conditions.
- Switch statements are slower due to the need for explicit type conversions.
Using a switch statement is often more performant when dealing with numerous conditions because it uses direct lookup tables, making it faster and more efficient than a series of if-else statements, which involve sequential comparisons.
You are developing a system where you have a base class "User" and two derived classes "Admin" and "Guest". If you want to add a method that is only applicable for "Admin" and not for "Guest", where should you add that method to adhere to the Liskov Substitution Principle?
- In the "User" class
- In the "Admin" class
- In the "Guest" class
- In a separate utility function
To adhere to the Liskov Substitution Principle, you should add the method specific to "Admin" in the "Admin" class. This ensures that each derived class (Admin and Guest) can be used interchangeably with the base class (User) without violating the principle.
You are designing a car simulation game using JavaScript. Each type of car (e.g., sedan, truck, etc.) has different methods for calculating fuel efficiency. Which object-oriented programming concept would be most appropriate to ensure that each car type can calculate fuel efficiency in its own way, while still inheriting basic characteristics from a general Car class?
- Inheritance
- Polymorphism
- Encapsulation
- Abstraction
Polymorphism allows different car types (e.g., sedan, truck) to have their own implementations of the fuel efficiency calculation method while inheriting common attributes and behaviors from the general Car class. This ensures flexibility and extensibility in your game.
A _________ function expression can be named, providing a reference to itself.
- Recursive
- IIFE (Immediately Invoked Function Expression)
- Anonymous
- Callback
A recursive function expression can be named, allowing it to refer to itself by name. This is useful in cases where a function needs to call itself during its execution, typically seen in solving recursive problems or implementing recursive algorithms.
The method getElementById selects an element using its ______.
- ID
- Tag Name
- Class Name
- Attribute
The method getElementById selects an element by its ID attribute. It returns the element with the specified ID attribute, providing a direct and efficient way to access an element in the DOM by its unique identifier.