Exam 2 (Ch. 9-15)
Which date string matches the pattern that follows? /^[01]?\d\/[0-3]\d\/\d{4}$/
"02/34/1968"
Which character is used to signify the end of the string in a PHP regular expression?
$
Which parameter of the session_set_cookie_params() function can be set so the cookie is only available on an HTTPS connection?
$secure
Which pattern can be used to validate a five-digit zip code:?
/^\d{5}$/
Which of the following will match a new line in a PHP regular expression?
/n
Which of the following will match a carriage return in a PHP regular expression?
/r
Which of the following will match a tab in a PHP regular expression?
/t
If the date is December 25, 2017, what output would be produced by the code below? echo date('n/j/Y');
12/25/2017
If the date is December 25, 2017, what output would be produced by the code below? echo date('n/d/Y h:i A');
12/25/2017 12:00 AM
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
If a match is found when the preg_split() function is executed, it returns
An array of the substrings that are created by removing the matches
What output would be produced by the code below? $names = array('Ron Burgundy', 'Brick Tamland', 'Champ Kind', 'Veronica Corningstone', 'Brian Fantana'); echo $names[1];
Brick Tamland
To call a static method named randomize in a class named Cart, you code
Cart::randomize()
________________ provide a way for a web application to store information in the user's web browser and retrieve it every time the user requests a page.
Cookies
If the date is December 25, 2017, what output would be produced by the code below? echo date('M. d, Y');
Dec. 25, 2017
If the date is December 25, 2017, what output would be produced by the code below? echo date('F d, Y');
December 25, 2017
________________ provides a way to create a new class based on an existing class.
Inheritance
What is stored in $message by the code that follows? $message = "The file is in \"C:\\My Documents\"";
The file is in "C:\My Documents"
Which of the following is NOT true about type declarations?
They were introduced with PHP 7.
Which character is used to signify the beginning or the end of a word in a PHP regular expression?
\B
Which of the following will match any character that is NOT a letter, number, or underscore in a PHP regular expression?
\W
Which of the following will match any digit in a PHP regular expression?
\d
Which of the following will match any letter, number, or underscore in a PHP regular expression?
\w
Which character is used to signify the beginning of the string in a PHP regular expression?
^
You can use PHP 7 and later to catch and handle
all fatal errors that are recoverable
In PHP, function name duplications are likely to occur because
all functions have global scope
Which of the following is a PHP function for sorting arrays?
all of these
Which of the following is a parameter of the setcookie() function?
all of these ($name, $value, $expire)
Which PHP function returns an array with the elements of two or more arrays in one array?
array_merge()
Regular expressions
can be used to completely validate some types of user entries
If you want to code a catch block that catches all types of exceptions and gets the message associated with the exception, you start the catch block like this:
catch (Exception $e) {
Which PHP function can be used to check if a specified class has been defined?
class_exists()
Which of the following is a nested function that has access to the outer function's local variables?
closure
Which of the following is a special method executed when a new object is created from a class?
constructor
Which PHP function returns the number of elements in an array?
count()
A fundamental concept of object-oriented programming that groups related data and functionality in a class is called ____________________.
encapsulation
Runtime errors that occur due to unexpected error conditions are called ________________.
exceptions
Suppose an array of country codes and country names is created with a statement like this: $country_codes = array('DEU' => 'Germany', 'JPN' => 'Japan', 'ARG' => 'Argentina', 'USA' => 'United States'); If you want to use echo statements to display a table of all of the codes and names, you could code those statements within a loop that starts like this:
foreach ($country_codes as $code => $name) {
All of the arguments that are passed to a function are available in an array that can be accessed by using the
func_get_args function
To create a function named randomize that has one parameter that has a default value of 5 and one required parameter that's passed by reference, you start the function like this:
function randomize(&$parm1, $parm2 = 5) {
To get the message that's related to an exception, you use the
getMessage() method of the exception object
Which PHP function returns a string with all special HTML converted to the HTML entity?
htmlentities()
What modifier can be added to the end of a regular expression in PHP to make it case insensitive?
i
Which of the following is a term that describes the process of creating an object from a class?
instantiation
The $_SESSION variable for a session
is an associative array
Which PHP function can be used to determine if an object is an instance of a class?
is_a()
The ________________ of a string specifies the number of characters that it contains.
length
A variable declared inside a PHP function would be considered a ____________ variable.
local
To create an object from a class named Cart that requires two arguments, you code
new Cart($arg1, $arg2)
What output would be produced by the code that follows? $names = array('Ron Burgundy', 'Brick Tamland', 'Champ Kind', 'Veronica Corningstone', 'Brian Fantana'); echo $names[5];
none of these
When you use object-oriented techniques to implement the MVC pattern, the methods of the model return the data as either arrays or
objects
Which function searches for a regular expression in a string and returns 1 if the pattern is found?
preg_match()
By default, the properties of a PHP class are ____________.
public
Which type of property in a class can be directly accessed by code outside of the class?
public
Which of the following are coded patterns that you can use to search for matching patterns in text strings?
regular expressions
If you want to round a number within an arithmetic expression, which function should you use?
round()
Which PHP function is used to create a cookie?
setcookie()
A ________________ is a special type of array that implements a last-in, first-out collection of values.
stack
A list of functions or methods in the reverse order in which they were called is a/an ________________.
stack trace
Gaps can be introduced into an array in all of the ways that follow, exept one. Which one is it?
store an empty value in an array
Which PHP function returns the number of characters in a string?
strlen()
The easiest way to add the values in an array is to use
the array_sum() function
When you use the substr() function to extract a substring from a string, you need to at least pass an argument that gives
the starting position of the substring
If you want to create an exception within a data-validation function and throw it to the statement that called the function, you code
throw new Exception("Validation error");
A ______________ is an integer that represents a date and time as the number of seconds since midnight, January 1, 1970.
timestamp
By default, all arguments passed to PHP functions are passed by ________________.
value
To get the value of a cookie, you can use the autoglobal ______________ variable.
$_COOKIE
Once a session is started, which superglobal variable can be used to get and set the user's data for a session?
$_SESSION
Which of the following statements is NOT a valid way to create a timestamp named $birthdate that represents December 2, 1969?
$birthdate = mktime(12, 2, 1969);
Suppose an associative array is added to a regular array by these statements: $cart = array(); $item = array(); $item['itemCode'] = 123; $item['itemName'] = "Visual Basic"; $item['itemCost'] = 57.5; $item['itemQuantity'] = 5; $cart[] = $item; To refer to the "itemCost" element in the associative array that's within the regular array, you would use this code:
$cart[0]['itemCost']
To refer to a public property named categoryName for an object that's referenced by $category, you code
$category->categoryName
To create a DateTime object that represents a due date that's 90 days after the current date, you use the following code:
$days = new DateInterval('P90D'); $due_date = new DateTime(); $due_date = $due_date->add($days);
Suppose that $name contains a last name followed by a comma and a space, followed by a first name. Then, you can store the first and last names in variables with code like this:
$i = strpos($name, ', '); $last_name = substr($name, 0, $i); $first_name = substr($name, $i+2);
Given the code that follows, $employees = array(); $employee["name"] = "John Smith"; $employee["age"] = 29; which of these statements assigns "John Smith is 29" to $message?
$message = $employee["name"] . " is " . $employee["age"];
Suppose that $name contains a last name followed by a comma and a space, followed by a first name. Then, you can store the first and last names in variables with code like this:
$name = explode(', ', $name); $last_name = $name[0]; $first_name = $name[1];
If you want to generate a random number that ranges from 1000 to 500,000 as a multiple of 100 (1000, 1100, 1200, etc.), and store it in a variable named $number, you can use the code that follows.
$number = mt_rand(10,5000); $number *= 100;
You can use the ________________ variable within a class to refer to the current object that has been created from the class.
$this
Which character is used before an argument to specify that the variable is being passed to a PHP function by reference?
&
Which of the following is NOT an escape sequence used in double-quoted strings in PHP?
/c
Code example 10-1 $current_date = new DateTime(); $due_days_diff = $current_date->diff($due_date); if ($current_date > $due_date) { $overdue_message = $due_days_diff->format( '%y years, %m months, and %d days overdue.'); } (Refer to code example 10-1) If $due_date contains a DateTime object for a date that comes 1 month and 7 days before the date stored in the $current_date variable, what will $overdue_message contain when this code finishes executing:
0 years, 1 months, and 7 days overdue.
If the date is December 25, 2017, what output would be produced by the code below? echo date('n/j/y');
12/25/17
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] . "|"; }
141.95|212.95|312.95|10.95|
If a request is not sent, a session will end by default after ________________ have passed.
24 minutes
What is the default name of a session cookie in PHP?
PHPSESSID
______________ properties and methods are inherited by a sublass but not accessible outside of the class.
Protected
Given this function: $compare_function = function(float $left, float $right) { return $left <=> $right; }; what will happen when the following statement is executed? $result = $compare_function(12.5, 12);
The function will return 1.
Given this function: function get_tax(int $subtotal, float $tax_pct) : float { $tax = $subtotal * $tax_pct; return $tax; } what will happen when the following statement is executed if strict types mode is set? $tax = get_tax(10.5, .08);
The function will throw an exception because the subtotal isn't an int value.
Given this function: function get_tax(int $subtotal, float $tax_pct) : float { $tax = $subtotal * $tax_pct; return $tax; } what will happen when the following statement is executed if strict types mode isn't set? $tax = get_tax(10.5, .08);
The function will truncate $subtotal to 10 and return .80.
To avoid the duplication of function names, you can
Use namespaces
You can deal with gaps in an array in all but one of the following ways. Which one is it?
Use the array_fill() function to replace all gaps in the array with empty strings.
Timestamps will encounter a(n) _______ problem if they are not converted to DateTime objects.
Y2K38
Which of the following statements about associative arrays is NOT true?
You can use a foreach loop to access the values of an associative array but not the indexes
Which of the following statements about an array of arrays is true?
You can use both for loops and foreach loops to work with an array of arrays.
Code example 10-1 $current_date = new DateTime(); $due_days_diff = $current_date->diff($due_date); if ($current_date > $due_date) { $overdue_message = $due_days_diff->format( '%y years, %m months, and %d days overdue.'); } (Refer to code example 10-1) If $due_date contains a DateTime object, $due_date_diff will contain
a DateInterval object
When you use session tracking, each HTTP request includes
a cookie that stores the session ID
The function that follows returns function coin_toss() { if (mt_rand(0, 1) == 0) { $coin = 'heads'; } else { $coin = 'tails'; } return $coin; }
a value of either "heads" or "tails"
Which of the following is a way to assign a string to a variable in PHP?
all of these (single quotes, double quotes, heredoc)
Which of the following is considered a scalar value?
all of these (string literal, numeric literal, Boolean value)
Which type of PHP function does not have a name?
anonymous
When you call a function, you can pass ________________ as values that correspond with the parameters of the function.
arguments
Which of the following is a PHP function that is used to count the number of times each value in an array is used?
array_count_values()
Which of the following functions removes the next element in a LIFO array (also known as a stack)?
array_pop()
Which of the following is a PHP function that returns the sum of the elements in an array?
array_sum()
An array that uses strings as indexes in PHP is known as a/an ________________ array.
associative
Which method of passing a variable allows the function to change the value of the variable?
by reference
Which of the following is a function that is passed as an argument to another function?
callback
If you create a function that passes an argument by reference, the function
can change the original variable without using a return statement
A/an ________________ is a special method within a class that is executed when a new object is created from the class.
constructor
Which of the following functions would you use to define a constant that stores an array in PHP 7.0 or later?
define()
Which method of a DateTime object can be used to determine an amount of time between two dates or times?
diff()
A subclass can ____________ the superclass by adding new properties and methods.
extend
Variable substitution is carried out by what process that converts a variable's value to a string?
interpolation
The ________________ function returns the largest value of the two or more arguments that are passed to it.
max
When a new class extends a superclass, it inherits the properties and methods of the superclass. Then, it can
override the inherited methods
When creating functions, you can include ______________ that are located inside parentheses after the function name.
parameters
A cookie that does not expire when the user closes his or her browser is a(n) ________________ cookie.
persistent
Which of the following are variables that store the data for a class?
properties
To code a constructor for a class named Cart that requires two arguments, you start with this code:
public function __construct($arg1, $arg2) {
To code a method within a class that sets the value for a private property named $name based on the argument that's passed to it, you start with this code:
public function setName($value) {
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
public methods to set and get their values
A "natural" comparison of two values
puts both "09" and "9" before "10"
A ________________ is a special type of array that implements a first-in, first-out collection of values.
queue
Which of the following functions should you use to get a random number if security is a concern?
random_int()
Which term describes locations in the code that have access to a variable?
scope
Which PHP function can be used to end a session?
session_destroy()
Which PHP function is used to start a new session or resume a previous session?
session_start()
To delete a cookie, you
set the cookie's value to an empty string and its expiration date to a time in the past
Which of the following is a PHP function that shuffles the values in an array in a random order?
shuffle()
When you create a DateInterval object, you pass it one argument that specifies one or more of the following:
years, months, weeks, days, hours, minutes, seconds