1.
7. To delete a cookie, you
Correct Answer
D. D. set the cookie’s value to an empty string and its expiration date to a time in
the past
Explanation
To delete a cookie, you set the cookie's value to an empty string and its expiration date to a time in the past. This effectively removes the cookie from the user's browser as it will no longer have any value and will be considered expired. The browser will then automatically remove the cookie from its storage.
2.
8. When you use session tracking, each HTTP request includes
Correct Answer
B. B. a cookie that stores the session ID
Explanation
When you use session tracking, each HTTP request includes a cookie that stores the session ID. This cookie is used to identify the user's session on the server. By including the session ID in a cookie, the server can associate subsequent requests from the same client with the correct session. This allows the server to maintain stateful information for the user across multiple requests. Therefore, option b is the correct answer.
3.
9. The $_SESSION variable for a session
Correct Answer
B. B. is an associative array
Explanation
The $_SESSION variable for a session is an associative array. This means that it is a type of array where each element is identified by a unique key instead of a numerical index. In the case of $_SESSION, the keys are the names of the session variables, and the values are the data stored in those variables. This allows for easy access and manipulation of session data in PHP.
4.
10. If necessary, you can use pHP functions to do all but one of the following. Which one is it?
Correct Answer
D. D. get the data for a session ID
Explanation
PHP functions can be used to get the name of the session cookie (option a), get the session ID (option b), and generate a new session ID (option c). However, getting the data for a session ID (option d) requires accessing the session data through other means, such as using session variables or session superglobal arrays.
5.
11. The function that follows returns
function coin_toss() {
if (mt_rand(0, 1) == 0) {
$coin = 'heads';
} else {
$coin = 'tails';
}
return $coin;
}
Correct Answer
C. C. a value of either “heads” or “tails”
Explanation
The given function coin_toss() uses the mt_rand() function to generate a random number between 0 and 1. If the generated number is 0, it assigns the value "heads" to the variable $coin, otherwise it assigns the value "tails". Finally, it returns the value of $coin. Therefore, the function returns a value of either "heads" or "tails".
6.
All of the arguments that are passed to a function are available in an array that can be accessed
by using the
Correct Answer
C. C. func_get_args function
Explanation
The correct answer is c. func_get_args function. This function allows access to all the arguments passed to a function by returning them as an array. The $_ARGUMENTS and $_FUNCTION variables do not exist in PHP, and the func_num_args function only returns the number of arguments passed, not the actual arguments themselves.
7.
13. If you create a function that passes an argument by reference, the function
Correct Answer
A. A. can change the original variable without using a return statement
Explanation
When a function passes an argument by reference, it means that the function can directly modify the original variable that was passed as an argument. This is done without needing to use a return statement to pass the modified variable back to the calling code. Therefore, option a. is correct as it states that the function can change the original variable without using a return statement.
8.
14. In pHP, function name duplications are likely to occur because
Correct Answer
C. C. all functions have global scope
Explanation
In PHP, function name duplications are likely to occur because all functions have global scope. This means that functions can be accessed and called from anywhere in the code, leading to a higher chance of naming conflicts. If multiple functions with the same name are defined in different parts of the code, it can cause confusion and unexpected behavior. It is good practice to use unique and descriptive names for functions to avoid such conflicts.
9.
15. To avoid the duplication of function names, you can
Correct Answer
A. Use namespaces
Explanation
To avoid the duplication of function names, using namespaces is an effective solution. Namespaces provide a way to group related functions, classes, and variables under a specific name, ensuring that they do not clash with similar names in other parts of the codebase. This helps in organizing and managing code, preventing naming conflicts, and improving code reusability. By using namespaces, developers can differentiate between functions with the same name by specifying the namespace they belong to, thus eliminating duplication and enhancing code clarity and maintainability.
10.
11. To prevent other classes from directly accessing the properties of a class, you can code them as
private. Then, to make them available to other classes, you can code
Correct Answer
B. B. public methods to set and get their values
Explanation
To prevent other classes from directly accessing the properties of a class, the properties can be coded as private. However, to make them available to other classes, public methods need to be coded to set and get their values. By using public methods, other classes can access and modify the values of the private properties indirectly, ensuring encapsulation and data integrity.
11.
When you use object-oriented techniques to implement the MVC pattern, the methods of the
model return the data as either arrays or
Correct Answer
B. B. objects
Explanation
When using object-oriented techniques to implement the MVC pattern, the methods of the model return the data as objects. Objects are instances of classes and they encapsulate both data and behavior. By returning data as objects, the model can provide a more structured and organized way of representing and manipulating the data, allowing for easier integration and interaction with other components of the MVC architecture.
12.
When a new class extends a superclass, it inherits the properties and methods of the superclass.
Then, it can
Correct Answer
B. B. override the inherited methods
Explanation
When a new class extends a superclass, it inherits the properties and methods of the superclass. This means that the new class can access and use these properties and methods without having to redefine them. However, if the new class wants to change the behavior of a method that it inherited from the superclass, it can override that method. By overriding a method, the new class provides its own implementation of the method, which will be used instead of the implementation in the superclass. This allows the new class to customize the behavior of the inherited method to better suit its own needs. Therefore, the correct answer is b. override the inherited methods.
13.
To code a constructor for a class named Cart that requires two arguments, you start with this code:
Correct Answer
B. B. public function __construct($arg1, $arg2) {
Explanation
The correct answer is b. public function __construct($arg1, $arg2) { because in PHP, the constructor for a class is defined using the __construct keyword. This code snippet shows the correct syntax for creating a constructor for the Cart class that requires two arguments, $arg1 and $arg2. The public access modifier indicates that the constructor can be accessed from outside the class.
14.
15. To create an object from a class named Cart that requires two arguments, you code
Correct Answer
A. A. new Cart($arg1, $arg2)
Explanation
To create an object from a class named Cart that requires two arguments, the correct way to code it is by using the "new" keyword followed by the class name and passing the arguments within parentheses. In this case, the arguments are represented as $arg1 and $arg2. Therefore, the correct code would be "new Cart($arg1, $arg2)".
15.
9. Which function searches for a regular expression in a string and returns 1 if the pattern is found?
Correct Answer
B. B. preg_match
Explanation
The function preg_match is used to search for a regular expression in a string and returns 1 if the pattern is found.
16.
If a match is found in a global search for a regular expression, the function returns
Correct Answer
D. A count of the number of matches and an array that contains all of the matches
Explanation
If a match is found in a global search for a regular expression, the function returns a count of the number of matches and an array that contains all of the matches. This means that the function not only provides information about the number of matches found, but also provides the actual matches themselves in an array format.
17.
If a match is found when the preg_split function is executed, it returns
Correct Answer
C. C. An array of the substrings that are created by removing the matches
Explanation
When the preg_split function is executed and a match is found, it returns an array of the substrings that are created by removing the matches. This means that the original string is split into multiple substrings at the locations where the matches are found, and these substrings are stored in an array. The matches themselves are not included in the resulting array.
18.
12. Regular expressions can be used to
Correct Answer
D. D. are limited to the validation of string data
Explanation
Regular expressions are a powerful tool for pattern matching and manipulating string data. They can be used to validate whether a given string matches a specific pattern or format. However, they are limited to the validation of string data only and cannot be used to validate other types of user entries such as numbers, dates, or email addresses. Therefore, option d is the correct answer as it accurately states the limitation of regular expressions to the validation of string data.
19.
13. To get the message that’s related to an exception, you use the
Correct Answer
B. B. getMessage method of the exception object
Explanation
The correct answer is b. getMessage method of the exception object. This method is used to retrieve the message associated with an exception object. It returns a string that provides information about the exception that occurred.
20.
An exception is thrown when
Correct Answer
D. D. a runtime error occurs
Explanation
When a runtime error occurs, the program encounters an exceptional situation that it cannot handle. As a result, an exception is thrown, indicating that an error has occurred. This allows the program to transfer control to an appropriate catch block, where the error can be handled or logged. The other options (a, b, and c) are not accurate explanations for when an exception is thrown.
21.
This pattern can be used to validate a five-digit zip code:
Correct Answer
C. C. /^\d{5}$/
Explanation
The correct answer is c. /^\d{5}$/.
This regular expression pattern uses the caret (^) and dollar sign ($) anchors to match exactly five digits. The \d represents any digit, and the {5} quantifier specifies that there should be exactly five occurrences of the preceding digit. The ^ at the beginning ensures that the pattern starts at the beginning of the string, and the $ at the end ensures that it ends at the end of the string. Therefore, this pattern will only match strings that consist of exactly five digits, which is the format of a five-digit zip code.
22.
9. Which of the following statements about associative arrays is NOT true?
Correct Answer
C. C. You can use a foreach loop to access the values of an associative array but not the indexes
Explanation
The given statement is false. In a foreach loop, you can access both the indexes and values of an associative array. The foreach loop iterates over each element in the array, allowing you to access both the key (index) and the value associated with that key. Therefore, option c is not true.
23.
10. Gaps can be introduced into an array in all of the ways that follow, except one. Which one is it?
Correct Answer
B. B. store an empty value in an array
Explanation
Gaps can be introduced into an array by adding a new element with an index that’s beyond the next one that’s available, deleting or unsetting an element from an array, and storing a NULL value in an array. However, storing an empty value in an array does not introduce a gap, as the element still exists in the array with an empty value.
24.
11. You can deal with gaps in an array in all but one of the following ways. Which one is it?
Correct Answer
D. D. Use the array_fill function to replace all gaps in the array with empty strings.
Explanation
The correct answer is d. Using the array_fill function to replace all gaps in the array with empty strings. This is the only option that specifically addresses filling the gaps in the array with empty strings. The other options either remove the gaps or skip the elements that contain nulls, but they don't explicitly fill the gaps with empty strings.
25.
Which of the following functions removes the next element in a LIFO array (also known as a stack)?
Correct Answer
D. D. array_pop
Explanation
The function array_pop removes the next element in a LIFO array, also known as a stack. In a stack, the last element that was added is the first one to be removed, which is the behavior of the array_pop function. The other options, array_shift, array_unshift, and array_push, do not remove elements from the end of the array, making them incorrect choices for this question.
26.
The easiest way to add the values in an array is to use
Correct Answer
C. C. the array_sum function
Explanation
The array_sum function is the easiest way to add the values in an array because it is a built-in function in many programming languages that allows you to calculate the sum of all the elements in an array with just one line of code. This function eliminates the need for writing a for loop or a foreach loop to iterate through the array and manually add up the values. Therefore, option c is the correct answer.
27.
14. Which of the following statements about an array of arrays is true?
Correct Answer
D. D. You can use both for loops and foreach loops to work with an array of arrays.
Explanation
The correct answer is d. You can use both for loops and foreach loops to work with an array of arrays. This means that you have the flexibility to choose between using a for loop or a foreach loop when working with an array of arrays. Both types of loops can be used to iterate through the elements of the outer array and the inner arrays within it.
28.
15. What will the value of the $totals_string variable be after the following code is executed?
$totals = array(141.95, 212.95, 411, 10.95);
$totals[2] = 312.95;
$totals_string = "";
for ($i = 0; $i < count($totals); $i++) {
$totals_string .= $totals[$i] . "|";
}
Correct Answer
B. B. 141.95|212.95|312.95|10.95|
Explanation
The code initializes an empty string variable called $totals_string. It then loops through the $totals array and appends each element followed by a "|" to the $totals_string variable. The loop iterates 4 times, so the $totals_string variable will contain the values of the $totals array separated by "|" symbols. Therefore, the correct answer is b. 141.95|212.95|312.95|10.95|.