Which design pattern can be used to create a family of related or dependent objects without specifying their concrete classes?
- Factory Method Pattern
- Abstract Factory Pattern
- Singleton Pattern
- Prototype Pattern
The Abstract Factory Pattern allows you to create families of related or dependent objects without specifying their concrete classes. It provides an interface for creating objects in various categories while ensuring their compatibility within the family.
What is the time complexity of the unshift() method in JavaScript arrays?
- O(n)
- O(1)
- O(log n)
- O(n log n)
The unshift() method in JavaScript arrays has a time complexity of O(n), where "n" represents the number of elements in the array. This is because it needs to shift all existing elements to make room for the new element at the beginning. The higher the number of elements, the longer it takes.
You are debugging a web page and find that an element isn't being selected as expected with document.querySelector('.example'). What could be a possible reason if the class name is correct?
- Element not present in the DOM
- Element is inside an iframe
- JavaScript not loaded before the query
- Class name contains special characters or spaces
If an element with the correct class name isn't being selected with document.querySelector('.example'), a possible reason could be that the element is not present in the DOM at the time of the query. Ensure that the element you are trying to select exists and is rendered when your JavaScript runs. The other options are less likely causes.
How to declare a constant array in JavaScript?
- const arr = [];
- const arr = new Array();
- const arr = {}
- const arr = [1, 2, 3];
To declare a constant array in JavaScript, you can use the const keyword followed by square brackets, like const arr = [];. The const keyword ensures that the variable arr cannot be reassigned, making it a constant array.
Imagine you are implementing a feature to upload a file to the server. Which HTTP method would be most appropriate to use with the Fetch API for this purpose?
- GET
- POST
- PUT
- DELETE
To upload a file to the server, you would use the POST HTTP method with the Fetch API. The POST method is used for submitting data to be processed to a specified resource, which is suitable for file uploads. GET, PUT, and DELETE have different purposes.
When using a for...of loop with strings, each iteration will provide a single _______.
- Character
- Word
- Line
- Number
When using a for...of loop with strings, each iteration will provide a single character. This loop is useful for breaking down strings into individual characters for various processing tasks, such as counting characters or manipulating them individually.
The switch statement evaluates expressions based on their _________.
- Values
- Cases
- Conditions
- Labels
The switch statement in JavaScript evaluates expressions based on their cases. Each case represents a possible value that the expression can take, and the code block associated with the matching case is executed. This allows for multiple execution paths based on different values of the expression.
In JavaScript, the _______ method is used to create a new object using an existing object as the prototype of the newly created object.
- Object.create()
- Object.assign()
- Object.extend()
- Object.instantiate()
In JavaScript, the Object.create() method is used to create a new object with the specified prototype object. It allows you to create objects that inherit properties and methods from an existing object, making it a powerful tool for prototypal inheritance.
In JavaScript, the ________ function is often used for delaying the execution of a function in an asynchronous manner.
- setTimeout
- setInterval
- asyncWait
- delayFunction
In JavaScript, the setTimeout function is often used for delaying the execution of a function in an asynchronous manner. It schedules the execution of a function after a specified time delay, allowing for tasks like animations or asynchronous operations to be handled.
Which array method adds elements to the beginning of an array?
- push()
- unshift()
- concat()
- splice()
The unshift() method is used to add elements to the beginning of an array. It's particularly useful when you want to insert one or more elements at the start of an existing array without affecting the order of the existing elements. Unlike push(), which adds elements to the end, unshift() works at the beginning.
What is a closure in JavaScript?
- A secure way to store passwords
- A private function
- A way to handle exceptions
- A function that remembers its lexical scope
A closure in JavaScript is a function that "remembers" its lexical scope, even when it's executed outside that scope. This allows the function to maintain access to variables from its parent scope, creating a powerful mechanism for encapsulation and data privacy.
In which context does the "this" keyword not refer to the object that calls the function?
- Global context
- Method context
- Function context
- Constructor context
The "this" keyword in JavaScript does not refer to the object that calls the function in the global context. In the global context, "this" points to the global object, which is usually the "window" object in browsers. This can be a source of confusion, so it's essential to understand the various contexts in which "this" behaves differently.