296 PHP QUIZ
How many lines will these statements store in the file referenced by the variable fputs($someFile, "Test"); fputs($someFile, "One, two, three");
1
If a variable named $carsSold contains the value 15 what is stored in $bonus after this code is processed? (Consider: are these structures separate or chained?) if ($carsSold < 20) $bonus = 50.00; if ($carsSold < 30) $bonus = 100.00; if ($carsSold < 40) $bonus = 200.00
200.00
Consider the following code. What bonus will be assigned to an employee who has worked 5 years and earns 16000.00? if ($yearsWorked < 5) { if ($salary < 18000.00) $bonus = 500.00; else $bonus = 400.00; } else $bonus = 750.00;
750
What value will be displayed? $result = 0; for ($count = 1; $count < 5; $count = $count + 1) { if ($count < 3) $result = $count; else $result = $result + $count; } print ("<p>RESULT: $result</p>")
9
If a variable named $score initially contains the value 90 what value is stored in $score after this code is processed? if ($score > 90 ) $bonus = 10; elseif ($score > 80 $bonus = 5; else $bonus = 0; $score = $score + $bonus; if ($score > 100) $score = 100;
95
Where must the PHP code be located inside a .php file?
<?php and ?>
Read this carefully. What HTML code is generated by the print statement? $numCourses = 4; $studyTime = $numcourses * 5; print ("<p>Allow $studyTime hours for study</p>");
<p>allow 0 hours for study</p>
What HTML output will be generated by the following PHP code? $inventory = 47; if ($inventory < 40) print("<p>Time to re-order!</p>"); else { $countdown = $inventory - 40; print("<p>Countdown to re-order: $countdown.</p>");
<p>countdown to re-order 7 </p>
Which of the following is NOT a relational operator?
=
PHP variable names must begin with
a dollar sign $
The following code processes a file containing five positive numbers. What will the variable $result contain after the code is executed? $result = 0; $someFile = fopen("some-file.txt", "r"); for ($count = 1; $count <= 5; $count = $count + 1) { $nextNum = fgets($someFile); if ($nextNum > $result) $result = $nextNum; } fclose($someFile); print ("<p>The result is $result</p>");
highest of the five numbers in the file
Consider the following address: C:\Projects\Website\index.html What is the name of the file?
index.html
Consider the following address: C:\Projects\Website\index.html Where is the Website folder located?
inside project folder
Which of the following is true about an algorithm?
instructions in algorithm must be clear & unambiguous
Which function can be used to test whether or not a variable has been created?
isset()
A file named member-list.txt contains the string "Mary:King" on a single line. Assume you already have written the following code: $listFile =fopen("member-list.txt","r"); $someValue = fgets($listFile); fclose($listFile); Which of the following lines must you add to correctly parse the string and store the first and last name into variables $firstName and $lastName?
list($firstName, $lastName) = explode (":", $someValue);
The fgets() statement is missing from this loop? What will happen? $numbersFile =fopen("numbers.txt","r"); $number = fgets($numbersFile); while (!feof($numbersFile)) { print ("<p>$number</p>"); } fclose($numbersFile);
loop will keep printing the first number in the file
What value is stored in $result after the following statements are processed? $sales = array(200, 300, 200, 400, 100); $result = $sales[0]; for ($i = 1; $i < sizeof($sales); $i = $i + 1) { if ($sales[$i] < $result) $result = $sales[$i];
lowest number stored in the $sales array
HTML is an example of:
markup language
Consider the following code: $listFile =fopen("member-list.txt","r"); $someValue = fgets($listFile); fclose($listFile); Assume member-list.txt contains the following two lines of text: Mary:King Peter:Jones What does $someValue contain after these statements are executed?
mary:king
What is wrong with the following code which is designed to read and display three numbers stored in a file? $someFile = fopen("some-file.txt", "r"); for ($count = 1; $count <= 3; $count = $count + 1) { $nextNum = fgets($someFile); print("$nextNum<br />"); fclose($someFile); }
file should be closed AFTER the FOR loop
What can you determine about the contents of some-data.txt if you know that the following code is correct? $value = 0; $someFile =fopen("some-data.txt","r"); $nextItem = fgets($someFile); while (!feof($someFile)) { $value = $value + $nextItem; $nextItem = fgets($someFile); } fclose($someFile); print ("<p>$value </p>");
files contains numbers, one on each line
Consider the following address: C:\Projects\Website\index.html What statement is true?
files is stored in the folder named website
What is wrong with this code? $numbersFile =fopen("numbers.txt","r"); $number = fgets($numbersFile); while (!feof($numbersFile)) { $number = fgets($numbersFile); print ("<p>$number</p>"); } fclose($numbersFile);
first number will not display because 2 statements are in wrong order
Which FOR loop heading will cause the loop to repeat the number of times stored in the variable $max?
for ($count = 1; $count <= $max; $count = $count + 1)
How can the following FOR loop be rewritten as a FOREACH loop? for ($i = 0; $i < sizeof($sales); $i = $i + 1) print ("<p>$sales[$i]</p>");
foreach ($sales as $nextSale) print ("<p>$nextSale</p>");
Which of the following is an example of a standard associative array that has been discussed in this chapter?
$_SESSION
Which of the following would refer to the value stored in the second element of an array named $sales?
$sales {1}
Which assertion is true concerning the following statement?
'sleeps2' is a key and 175.75 is a value.
If $hourlyWage contains the value 20.00 and $hoursWorked contains the value 30, what value will $bonus contain after the following code is executed?
25
How many COLUMNS will be displayed in this table? print ("<table border = \"1\">"); for ($count = 1; $count <=10; $count = $count + 1) { $result1 =$count * 10; $result2 =$count * 100; print("<tr><td>$count</td><td>$result1</td> <td>$result2</td></tr>"); } print ("</table");
3
How many different paths are there through this code segment? if ($carsSold < 20) $bonus = 50.00; elseif ($carsSold < 30) $bonus = 100.00; else $bonus = 200.00;
3
How many times will the following loop repeat? for ($count = 0; $count < 3; $count = $count + 1) { print ("<p>Hello!</p>"); }
3
How many times will the following loop repeat? for ($count = 1; $count <= 3; $count = $count + 1) { print ("<p>Hello!</p>"); }
3
How many rows and columns will be displayed by this table? <table border = "1" > <tr><td>France</td><td>Paris</td></tr> <tr><td>England</td><td>London</td></tr> <tr><td>Italy</td><td>Rome</td></tr> </table>
3 rows & 2 columns
What value is stored in $result after these PHP instructions are executed? $value1 = 15; $value2 = 10; if ($value1 > $value2) $value2 = 15; $result = $value1 + $value2; print("<p>The result is $result</p>");
30
How many different paths are there through a program that contains an IF..ELSE structure, followed by a (separate) IF structure?
4
How many possible paths are there through the following code segment? if ($jobTitle == "Manager") $weeklyPay = $salary/52; else $weeklyPay = $hourlyWage * 40; if ($weeklyPay < 500) $bonus = 100; else $bonus = 50; $weeklyPay = $weeklyPay + $bonus;
4
What value is stored in $sales[1] after the following statements are all processed in order? $sales [0] = 200; $sales [1] = $sales [0] + 100; $sales [2] = $sales [1] - sales[0]; $sales [1] = $sales [2] + $sales [1]; $sales [0] = 500;
400
What will be displayed if this algorithm is executed? oldInventory = 520 unitsSold = 30 newInventory = oldInventory - unitsSold display newInventory
490
How many lines will these statements store in the file referenced by the variable fputs($someFile, "Test\n\n"); fputs($someFile, "\nOne,\ntwo,\n three");
6
What is the purpose of the PHP empty() function?
Determines whether or not a variable contains a value.
Consider the following code? Which employees will get a 400.00 bonus? if ($yearsWorked < 5) { if ($salary < 18000.00) $bonus = 500.00; else $bonus = 400.00; } else $bonus = 750.00;
Employees who have worked less than 5 years, with a salary of at least 18000.00.
Which variable name is an example of camelback notation?
unitsSold
What is the purpose of a repetition structure?
Repeatedly execute one or more statements a certain number of times.
Which markup language was HTML initially based on?
SGML
Is the following an example of an absolute or relative address? <a href = "http://www.oursite.com/contact.html">Contact Us</a>
absolute address
What is a 2-dimensional array?
an array where each element contains an array
What type of operation is used to store a value in a variable?
assignment operation
an interpreter:
converts a program written in high level language into executable code one line at a time
What is important about persistent data?
data remains available after a program has ended
What is the purpose of the priming read?
determine whether or not file is empty before processing
Which of the following is NOT correct?
else section must begin with test condition
Loops are often used to repeatedly execute a group of instructions until a condition changes. What is the general term for this kind of loop?
event controlled loop
If a variable named $carsSold contains the value 25, what is the result of this test? if ($carsSold !- 25)
false
If a variable named $carsSold contains the value 25, what is the result of this test? if ($carssold > 25)
false
What languages will you learn in this course?
php & js
What is the priming read?
read statement occurs b4 a WHILE NOT EOF LOOP
What does this code actually do? $result = 0; $someFile =fopen("some-data.txt","r"); $nextItem = fgets($someFile); while (!feof($someFile)) { list($name, $booksOverdue) = explode(":", $nextItem); if ($booksOverdue > 0) $result = $result + 1; $nextItem = fgets($someFile); } fclose($someFile); print ("<p>$result </p>");
reads a file & counts the # of people who have books overdue
A discount store is selling its inventory of computer monitors for $100, $200, or $300 depending on screen size. Here's what a programmer wrote as a selection structure to display the price based on the size. What needs to change for this code to work correctly? if ($screenSize < 20) print ("<p>Price: $100.00</p>"); if ($screenSize< 25) print ("<p>Price: $200.00</p>"); else print ("<p>Price: $300.00</p>")
second if should be an elseif
The numbers.txt file contains a list of numbers, one on each line. What does this code do? $result = 0; $numbersFile =fopen("numbers.txt","r"); $number = fgets($numbersFile); while (!feof($numbersFile)) { if ($number > 0) $result = $result + $number; $number = fgets($numbersFile); } fclose($numbersFile); print (<p>RESULT: $result</p>");
sums the positive numbers
The following print statement is missing a closing parenthesis. Is this a syntax error or a logical error? print ("<p>I guess I have an error - I'm melting!"</p>;
syntax error
This loop is supposed to use a table to display the numbers 1, 2 and 3 along with the square of each number? What's wrong with this code? for ($count = 1; $count <=3; $count = $count + 1) { print ("<table border = \"1\">"); $result = pow($count, 2); print("<tr><td>$count</td><td>$result</td></tr>"); print ("</table");
the <table> and </table> tags should be located before and after the loop
What is wrong with the following piece of PHP code? if ($password = "xyz") print("<p>That's the correct password!</p>");
the = should be ==
Which statement is true?
the counting variable of a FOR loop does not have to be named $count
Study the following algorithm. The instruction "Turn a card over" appears twice. Why does it appear the first time, before the loop structure? Set count to 1 Turn a card over WHILE (card != Jack) Add 1 to count Turn a card over ENDWHILE Display count
this ensures that the loop condition has a card to test the very first time
Where should the session_start() function be used?
top of every page that is to participate in the session
True or False? An array can contain other arrays as array elements.
true
Which statement is false?
while loops can only be used to process files