Class INDEX     Text INDEX     Midterm     Cis52 INDEX

Martha's CIS 52 Final ANSWERS

Question #1 (244-2). Which of the following operators can be used with strings (Choose all that apply). ANSWER: "There are two string operators. The first is the concatenation operator ('.'), which returns the concatenation of its right and left arguments. The second is the concatenating assignment operator ('.='), which appends the argument on the right side to the argument on the left side." - PHP Manual
  1. .
  2. = =
  3. .=
  4. +=

Question #2 (244-4). What is the escape sequence for a single quotation mark? ANSWER: " To specify a literal single quote, escape it with a backslash (\). To specify a literal backslash before a single quote, or at the end of the string, double it (\\). Note that attempting to escape any other character will print the backslash too...Note: Unlike the two other syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings. " - PHP Manual

  1. \\
  2. \'
  3. \~
  4. There is no escape sequence for a single quotation mark.

Questions #3 (244-8). Which of the following functions returns the number of occurrences of a substring? String Functions-w3schools

  1. strlen() - Returns the length of a string - w3schools
  2. strspn() - Returns the number of characters found in a string that contains only characters from a specified charlist - w3schools
  3. substr_count() - Counts the number of times a substring occurs in a string - w3schools
  4. strcspn() - Returns the number of characters found in a string before any part of some specified characters are found - w3schools

Question #4 (245-12). Which of the followingfunctions allows you to replace characters within a specified portion of a string?

  1. str_ireplace() - Replaces some characters in a string (case-insensitive) - w3schools
  2. str_replace() - Replaces some characters in a string (case-sensitive) - w3schools
  3. substr_replace() - Replaces a part of a string with another string - w3schools
  4. strstr() - Translates certain characters in a string - w3schools

Question #5 (245-14). If you specify an empty string as the second argument of the strtok() function or if the string does not contain any of the separators you specify, the strtok() function returns a value of false. True or False? ANSWER: "it is only the first call to strtok() that uses the string argument. After the first call, this function only needs the split argument, as it keeps track of where it is in the current string. To tokenize a new string, call strtok() with the string argument again" - w3schools

ANSWER: brooklyn.cuny.edu - Strings(ppt) "The strtok() function returns the entire string if: An empty string is specified as the second argument of the strtok() functtion; The string does not contain any of the separators specified"

Question #6 (245-16). String comparison operators and most string comparison functions compare individual characters according to their ASCII position. True or False?   ANSWER: There is only one string operator in PHP. The concatenation operator (.)  is used to put two string values together.w3schools:

ANSWER: bin2hex(), chr(), count_chars(), ord() - specifically cite ASCII characters

ANSWER: brooklyn.cuny.edu - Strings (ppt) "Most string comparison functions compare strings based on their ASCII values." p. 36

Question #7 (246-18). You should always rely on JavaScript code to perform data validation. True or False?

ANSWER: "User input should be validated on the browser whenever possible (by client scripts). Browser validation is faster and reduces the server load. You should consider server validation if the user input will be inserted into a database. A good way to validate a form on the server is to post the form to itself, instead of jumping to a different page. The user will then get the error messages on the same page as the form. This makes it easier to discover the error." - PHP Forms (w3schools)

Question #8 (246-20). If you want to ensure that a form variable is of a specific numeric data type, such as an integer, you should first use the is_numeric() function to test whether the variable is numeric, then cast the variable to the required data type. True or False?

EXAMPLE: is_numeric - from plus2net

if (!is_numeric($quantity))
{
    echo 'is not numeric';
}

ANSWER: "Be careful of validation by type when dealing with $_GET and $_POST variables. If someone inputs a number, is_numeric will not be true, because all information passed as GET and POST vars are strings. So to find out if a valid number has been supplied, you can either cast and then see if it is greater than 0. Or alternatively, check the string to see if it is only made of 0-9 characters." Symic.com

Question #9 (299-2). Which of the following is the correct syntax for opening a handle for reading only to a text file named prospects.txt? p. 249 and File Open, Tizag.com

  1. $SalesProspects = fopen("prospects.txt", "r+"); "Opens a file so that it can be read from and written to. The file pointer is at the beginning of the file."
  2. $SalesProspects = open("prospects.txt", "w"); -"Open a file for write only use...The file pointer begins at the start of the file."
  3. $SalesProspects = fopen("prospects.txt", "r"); - "Open a file for read only use. The file pointer begins at the front of the file."
  4. $SalesProspects = fileopen("prospects.txt", "a"); - "Open a file for write only use. However, the data in the file is preserved and you begin will writing data at the end of the file. The file pointer begins at the end of the file."

Question #10 (299-4). A __________ is a special type of variable that refers to the currently selected line or character in a file.

  1. character pointer
  2. line pointer
  3. file pointer
  4. directory pointer

ANSWER: "A file pointer is PHP's way of remembering its location in a file. When you open a file for reading, the file pointer begins at the start of the file. This makes sense because you will usually be reading data from the front of the file...However, when you open a file for appending, the file pointer is at the end of the file, as you most likely will be appending data at the end of the file. When you use reading or writing functions they begin at the location specified by the file pointer." - File Open - Tizag.com

Question #11 (300-6). Which of the following escape sequences is used on Macintosh platforms?

ANSWER: Mac in the Shell - mactech.com

ANSWER 2: Programming Escape Characters - wilsonmar.com

  1. \n
  2. \r
  3. \n\r
  4. \r\n - Use this on Windows machines for outbound mail server - p. 214

Question #12 (300-8). Which of the following constants can you use with the file_put_contents function to append data to the end of a file:

ANSWER: PHP Manual

FILE_USE_INCLUDE_PATH Search for filename in the include directory. See include_path for more information.
FILE_APPEND If file filename already exists, append the data to the file instead of overwriting it. Mutually exclusive with LOCK_EX since appends are atomic and thus there is no reason to lock.
LOCK_EX Acquire an exclusive lock on the file while proceeding to the writing. Mutually exclusive with FILE_APPEND.
FILE_TEXT data is written in text mode. If unicode semantics are enabled, the default encoding is UTF-8. You can specify a different encoding by creating a custom context or by using the stream_default_encoding() to change the default. This flag cannot be used with FILE_BINARY. This flag is only available since PHP 6.
FILE_BINARY data will be written in binary mode. This is the default setting and cannot be used with FILE_TEXT. This flag is only available since PHP 6.


  1. INCLUDE_FILE
  2. FILE_USE_INCLUDE_PATH
  3. APPEND
  4. FILE-APPEND

Question #13 (300-10). What is the correct syntax for using the fwrite() function to assign a value "Forestville Foods" to a handle named $SalesProspects?

ANSWER: "fwrite() writes the contents of string to the file stream pointed to by handle." SYNTAX: int fwrite ( resource $handle , string $string [, int $length ] )" - PHP Manual;

EXAMPLE:
<?php
$fp = fopen('data.txt', 'w');
fwrite($fp, '1');
fwrite($fp, '23');
fclose($fp);

  1. $SalesProspects = fwrite("Forestville Foods\n");
  2. fwrite($SalesProspects, "Forestville Foods\n");
  3. fwrite("Forestville Foods\n", $SalesProspects);
  4. fwrite("$SalesProspectsForestville Foods\n");

Question #14 (300-12). Which of the following operational constants can you use with the flock() function? (Choose all that apply.)

ANSWER: "The flock() function locks or releases a file...This function returns TRUE on success or FALSE on failure." - w3schools.com - flock(file,lock,block). Possible values for lock (required):

  1. LOCK_EX - " Exclusive lock (writer). Prevent other processes from accessing the file"
  2. LOCK_NB - "Avoids blocking other processes while locking"
  3. LOCK_SH - "Shared lock (reader). Allow other processes to access the file"
  4. LOCK_UN - "Release a shared or exclusive lock"

Question #15 (301-14). The file() function automatically recognizes whether the lines in a text file end in \n, \r, or \r\n. True or False?

ANSWER: "The file() reads a file into an array...Each array element contains a line from the file, with newline still attached. - w3schools

ANSWER 2 - PHP Manual:

"Note: Each line in the resulting array will include the line ending, unless FILE_IGNORE_NEW_LINES is used, so you still need to use rtrim() if you do not want the line ending present.

Note: If PHP is not properly recognizing the line endings when reading files either on or created by a Macintosh computer, enabling the auto_detect_line_endings run-time configuration option may help resolve the problem."

Question #16 (301-16). Which of the following functions returns a value of true when a file pointer reaches the end of a file?

ANSWER: "What is a file pointer? It's the marker used by PHP to keep track of where the currently executing file operations take place. For instance, when a file is first created the file pointer is adjusted to the beginning of the file. Likewise, when a file is opened in append mode, the file is opened and the pointer points to the very end of the file." - Working with Files in PHP - O'Reilly.com

ANSWER: "The feof() function checks if the "end-of-file" (EOF) has been reached... The feof() function is useful for looping through data of unknown length...Note: You cannot read from files opened in w, a, and x mode!" - w3schools

  1. is_end()
  2. end()
  3. eof()
  4. feof()

Question #17 (301-18). Which of the following statements creates a directory named "students" at the same level as the current directory?

ANSWER: Microsoft Technet - "To create a directory named Taxes with a subdirectory named Property, which contains a subdirectory named Current, type: mkdir \Taxes\Property\Current. This is the same as typing the following sequence of commands with command extensions disabled:

mkdir \Taxes
chdir \Taxes
mkdir Property
chdir Property
mkdir Current"

  1. mkdir("\\students");
  2. mkdir("students");
  3. mkdir("\\students\\");
  4. mkdir("..\\students");

Question #18 (355-1). Which of the following functions remove the first element from the beginning of an array? w3schools; p. 155 Text

  1. array_shift() - "Removes the first element from an array, and returns the value of the removed element" - w3schools
  2. array_unshift() - "Adds one or more elements to the beginning of an array" - w3schools
  3. array_push() - "Inserts one or more elements to the end of an array" - w3schools
  4. array_pop() - "Deletes the last element of an array" - w3schools

Question #19 (355-3). Which of the following functions removes the last element from the end of an array? (Choose all that apply.)

  1. array_shift() - "Removes the first element from an array, and returns the value of the removed element" - w3schools
  2. array_unshift() - "Adds one or more elements to the beginning of an array" - w3schools
  3. array_push() - "Inserts one or more elements to the end of an array" - w3schools
  4. array_pop() - "Deletes the last element of an array" - w3schools

Question #20 (355-5). After removing elements from an array, the unset() function automatically renumbers the remaining elements. True or False?

ANSWER: "If you delete an element by unset (say unset($foo[3]) the remaining elements stay in their originally entered order" - Dev.shed.com

Question #21 (355-7). What is the correct syntax for declaring and initializing an associative array?

EXAMPLE 1: Webucator - PHP Tutorial: Arrays

$Beatles = array();
$Beatles['singer1'] = 'Paul'; 
$Beatles['singer2'] = 'John'; 
$Beatles['guitarist'] = 'George';  
$Beatles['drummer'] = 'Ringo';

EXAMPLE 2:

$Beatles = array('singer1' => 'John',                   
'singer2' => 'Paul',
'guitarist' => 'George',
'drummer' => 'Ringo');

 

  1. $AutoMakers = array("Ford" . "Mustang", "Chevrolet" . "Corvette"):
  2. $AutoMakers = array("Ford"="Mustang", "Chevrolet"="Corvette");
  3. $AutoMakers = array("Ford">"Mustang", "Chevrolet">"Corvette");
  4. $AutoMakers = array("Ford"=>"Mustang", "Chevrolet"=>"Corvette");

Question #22 (355-9).If you declare an array in PHP and use a starting index other than 0, empty elements are created for each index between 0 and the index value you specify. True or False? SOURCE: PHP Manual - Arrays

EXAMPLE: PHP Manual

<?php
$array = array(1, 1, 1, 1,  1, 8 => 1,  4 => 1, 19, 3 => 13);
print_r($array);
?>

The above example will output:

Array  (      [0] => 1      [1] => 1      [2] => 1      [3] => 13      [4] => 1      [8] => 1      [9] => 19  )  

ANSWER: "Syntax "index => values", separated by commas, define index and values. index may be of type string or integer. When index is omitted, an integer index is automatically generated, starting at 0. If index is an integer, next generated index will be the biggest integer index + 1. Note that when two identical index are defined, the last overwrite the first... Having a trailing comma after the last defined array entry, while unusual, is a valid syntax."

Question #23 (356-11). Which of the following functions returns the value of an element where an array's internal pointer is positioned? w3schools - Arrays

  1. current() - "Returns the current element in an array" - w3schools
  2. key() - "Fetches a key from an array" - w3schools
  3. array() - "Creates an array" - w3schools
  4. array_values() - "Returns all the values of an array" - w3schools

Question #24 (356-13). Which of the following locates a key named "Ford" in an array named $AutoMakers[]? see Eastern Illinois U

  1. array_key_exists($AutoMakers=>"Ford");
  2. $AutoMakers = array_key_exists("Ford");
  3. array_key_exists($AutoMakers, "Ford");
  4. array_key_exists("Ford", $AutoMakers);

Question #25 (356-15). Which of the following functions perform a reverse sort on an array? (Choose all that apply.). PHP Array Functions - w3schools

  1. asort(0 - Sorts an array and maintain index association; w3schools
  2. usort() - Sorts an array by values using a user-defined function - w3schools
  3. rsort() - Sorts an array in reverse order - w3schools
  4. krsort() - Sorts an array by key in reverse order - w3schools

Question #26 (356-17). If you use the array_merge() function with indexed arrays, all elements in on array are appended to another array and renumbered. True or False? ANSWER: Eastern Illinois U

Question #27 (409-1). A flat-file database consists of a single table. True or False?

Question #28 (409-3). What is the correct term for the individual pieces of information that are in a database record? ANSWER: Eastern Illinois U

  1. element
  2. field
  3. section
  4. container

Question #29 (410-9). What is the default value of the mysql command's -h argument? ANSWER: Eastern Illinois U

  1. database
  2. mysqlmonitor
  3. mysqladmin
  4. localhost

Question #30 (410-10). What character must terminate SQL commands in the MySQL Monitor? Eastern Illinois U

  1. colon (:)
  2. semicolon (;)
  3. ampersand (&)
  4. period (.)

Question #31 (410-11). With what characters do you quote identifiers that include special characters? ANSWER: Eastern Illinois U

  1. quotation marks (')
  2. double quotation marks (")
  3. backticks (`)
  4. tildes (~)

Question #32 (411-14). Which of the following statements displays the available databases in your MySQL installation? ANSWER: Eastern Illinois U

  1. SHOW DATABASES;
  2. SHOW DATABASES();
  3. LIST FILES;
  4. GET LIST();

Question #33 (411-15). What's the first thing you should do after creating a new database? ANSWER: Eastern Illinois U

  1. Save the database
  2. Restart the MySQL Monitor.
  3. Select the database.
  4. Create a table.

Question #34 (411-21). Which of the following is the correct string for a filter that narrows a recordset include only records in which the State filed is equal to Massachusetts? (Ignore double quotation marks that surround each answer choice.) ANSWER: Eastern Illinois U

  1. "WHERE State = 'Massachusetts' "
  2. "State = 'Massachusetts' "
  3. "WHERE `State` is Massachusetts"
  4. "`State` = 'Massachusetts' "

Question #35 (466-2). Which of the following functions closes a database connection?

ANSWER: http://ngtech.org/forums/MIS4530/comments.php?DiscussionID=35 says the answer is NOT mysqli_free()!

  1. close() - unknown
  2. mysqli_close() - "Closes a previously opened database connection" - PHP Manual
  3. mysqli_free()
  4. mysqli_free_connect()

Question #36 (467-4). What is the correct syntax for selecting a database with the mysqli_select_db() function?

ANSWER: Description: bool mysqli_select_db ( mysqli link, string dbname ) The mysqli_select_db() function selects the default database (specified by the dbname parameter) to be used when performing queries against the database connection represented by the link parameter. Note: This function should only be used to change the default database for the connection. You can select the default database with 4th parameter in mysqli_connect(). Return Values: Returns TRUE on success or FALSE on failure. (PHP Builder)

  1. mysqli_select_db() (connection) - needs db
  2. mysqli_select_db(database) - needs connection
  3. mysqli_select_db(connection, database)
  4. database = mysqli_select_db(connection)

Question #37 (467-6). The following code structure prevents error messages from printing in the event that the database connection is not available. True or False?

$DBConnect = mysqli_connect("localhost", "dongosselin", "rosebud", "flightlog");
if (!DBConnect)
     echo "<p>The database server is not available.</p>";
else {
      echo "<p>Successfully connected to the database server.</p>";
     mysqli_close($DBConnect);
}

TEST:

Successfully connected to the database server.

Question #38 (467-8). Which of the following characters suppresses error messages in PHP?

ANSWER: "PHP supports one error control operator: the at sign (@). When prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored. " - Error Control Operators, PHP Manual

  1. *
  2. &
  3. #
  4. @

Question #39 (468-10). Which is the following functions reports the error message from the last failed database connection attempt?

  1. mysqli_connect_errno() - "Returns the error code from last connect call" (PHP Manual)
  2. mysqli_connect_error() - "Returns a string description of the last connect error" (PHP Manual)
  3. mysqli_errno() - "Returns the error code for the most recent function call" - PHP Manual
  4. mysqli_error() - "Returns a string description of the last error" - PHP Manual

Question #40 (468-12). Which of the following functions returns the fields in the current row of a set into an indexed array?

  1. mysqli_datafetch() - unknown!
  2. mysqli_data_seek() -"...moves the internal row pointer of the MySQL result associated with the specified result identifier to point to the specified row number. The next call to a MySQL fetch function, such as mysql_fetch_assoc(), would return that row. row_number starts at 0. The row_number should be a value in the range from 0 to mysql_num_rows() - 1. However if the result set is empty (mysql_num_rows() == 0), a seek to 0 will fail with a E_WARNING and mysql_data_seek() will return FALSE. "- PHP Manual
  3. mysqli_index_row - unknown
  4. mysqli_fetch_row() - "Fetches one row of data from the result set and returns it as an enumerated array, where each column is stored in an array offset starting from 0 (zero). Each subsequent call to this function will return the next row within the result set, or NULL if there are no more rows. " - PHP Manual

Question #41 (469-18). Which of the following SQL keywords creates an autoincrementing field?

  1. AUTO
  2. INCREMENT
  3. AUTO_INCREMENT - "The AUTO_INCREMENT attribute can be used to generate a unique identity for new rows: " - dev.mysql.com
  4. AUTOINCREMENT

 

Question #42 (469-20). The __________ function returns the number of operations for various types of actions, depending on the type of query.

  1. mysqli_get_info() - unknown
  2. mysqli_operations() - unknown
  3. mysqli_info() - "Retrieves information about the most recently executed query" - PHP Manual
  4. mysqli_fetch_actions() - unknown

 

**************************************************************

**************************************************************