A(n) _______ function returns a promise.
- Synchronous
- Callback
- Async
- Static
An Async function returns a promise implicitly. When you declare a function as async, it means the function will always return a promise, allowing you to use await inside it to handle asynchronous operations more cleanly.
Which of the following is NOT an argument passed to an event listener when it is invoked?
- event
- target
- type
- timestamp
When an event listener is invoked, it receives an event object containing information about the event. It also has access to the target (the element that triggered the event) and type (the type of event). However, timestamp is not an argument passed to an event listener. It can be obtained using event.timeStamp.
What is the purpose of the this keyword inside a constructor function?
- Refers to the global object
- Refers to the parent object
- Refers to the current instance
- Refers to the next sibling
The this keyword inside a constructor function refers to the current instance of the object being created. It allows you to access and assign properties and methods to the instance being constructed, making it unique for each object created with the constructor. It does not refer to the global or parent object.
Which method would you use to add a new property to an object after it has been created?
- Object.add()
- Object.append()
- Object.assign()
- Object.insert()
To add a new property to an existing object in JavaScript, you would typically use the Object.assign() method. This method allows you to merge properties from one or more source objects into a target object. It can be used to add or update properties in the target object.
When using the new keyword to create an object, what kind of function must you use?
- Regular Function Declaration
- Arrow Function
- Constructor Function
- Prototype Function
When using the new keyword to create an object, you must use a Constructor Function. Constructor functions are used to create and initialize objects. They are invoked with the new keyword and often set object properties and methods within the constructor. Arrow functions are not suitable because they don't have their own this context.
The switch statement in JavaScript uses _________ comparison to evaluate cases.
- Strict (===)
- Loose (==)
- Greater (>)
- Lesser (<)
The switch statement in JavaScript uses strict (===) comparison to evaluate cases. This means that not only the value but also the data type must match for a case to be executed. This ensures accuracy when comparing values in a switch statement.
Imagine you’re refactoring a piece of code that involves a series of callback functions into a format that uses async/await. What should you pay extra attention to regarding error handling when refactoring the code?
- Ensure proper try/catch
- Maintain callback structure
- Avoid async/await
- Ignore error handling entirely
When refactoring code to use async/await, it's crucial to ensure proper error handling. This means using try/catch blocks around await operations to catch and handle any exceptions that may occur during asynchronous operations. Neglecting error handling could lead to unhandled exceptions and unexpected behavior.
How can we determine whether a PHP variable is an instantiated object of a certain class?
- instanceof operator
- is_object() function
- is_instance() function
- class_exists() function
To determine if a PHP variable is an instantiated object of a certain class, you can use the instanceof operator. It checks if an object is an instance of a specific class or has a class in its inheritance hierarchy. Learn more: http://php.net/manual/en/language.operators.type.php
What function is used to read the contents of a file in PHP?
- readfile()
- file_get_contents()
- fread()
- include()
In PHP, the fread() function is used to read the contents of a file. It takes the file handle obtained from fopen() as the first parameter and the maximum number of bytes to read as the second parameter. This function returns the content of the file as a string. Alternatively, you can use file_get_contents() to read the entire file into a string or other file reading functions depending on your specific use case.
What can be potential issues when working with Regular Expressions in PHP?
- Performance issues when processing large strings or complex patterns.
- Security vulnerabilities due to inadequate input validation and sanitization.
- Difficulty in understanding and writing complex Regular Expressions.
- Limited support for Unicode characters and multibyte strings.
Potential issues when working with Regular Expressions in PHP can include performance concerns when processing large strings or complex patterns. Regular Expressions can be resource-intensive, so it's important to optimize them for better performance. Security vulnerabilities can arise when input validation and sanitization are not properly implemented, leading to potential attacks like Regular Expression Denial of Service (ReDoS) or injection attacks. Writing and understanding complex Regular Expressions can also be challenging, especially when dealing with intricate patterns. Additionally, PHP's support for Unicode characters and multibyte strings in Regular Expressions may have limitations, requiring additional considerations. Learn more: https://www.php.net/manual/en/book.regex.php
Which of the following are true about associative arrays in PHP?
- Associative arrays use numeric keys to access elements.
- Associative arrays preserve the order of elements.
- Associative arrays can have elements of different data types.
- Associative arrays can only store a single value.
The correct option is 3. Associative arrays in PHP use string or integer keys to access elements, not numeric keys. Unlike indexed arrays, associative arrays do not preserve the order of elements as they are accessed using the keys. Associative arrays can indeed store elements of different data types, allowing for flexible data representation. They are suitable for organizing and accessing data based on meaningful labels or identifiers. Associative arrays can store multiple key-value pairs, making them suitable for representing more complex data structures. Learn more: https://www.php.net/manual/en/language.types.array.php#language.types.array.syntax
What’s the difference between __sleep and __wakeup?
- __sleep serializes
- __wakeup serializes
- Both are the same
- __wakeup unserializes
__sleep is called before an object is serialized, allowing you to define which data should be serialized. __wakeup is called after unserialization. Learn more: http://php.net/manual/en/language.oop5.magic.php