CS 6515 COMPREHENSIVE EXAM SCRIPT COMPLETE QUESTIONS VERIFIED SOLUTIONS
Question:
Referring to the figures above, if the user submitted the name as "<h1>Pam</h1>" and howmany as
4, the browser would display: "Pam ordered 4 widgets."
Answer:
False (htmlspecialchars() converts < into < and > into >, so the echo statement outputs
"<p><h1>Pam</h1> ordered 4 widgets.</p>". The browser displays < and > as <
and >, so the browser displays "<h1>Pam</h1> ordered 4 widgets.")
Question:
An attacker could use an XSS attack to redirect the user's browser to a malicious website.
Answer:
True (JavaScript in an XSS attack can redirect a browser to any URL.)
Question:
The following code uses the getData() function from the figure above and provides sufficient data
validation for a 16 digit credit card number. $creditCardNum = getData("creditCardNum"); if
(strlen($creditCardNum) == 16) { // Valid number } else { // Invalid number }
Answer:
False (The code does not ensure that $creditCardNum contains only digits, and $creditCardNum
could contain spaces between the numbers, so the length could be longer than 16 characters. A
regular expression (discussed elsewhere) is ideal for checking text against an expected pattern.)
Question:
What is $_SERVER["REQUEST_METHOD"] the first time a script is loaded in the web browser?
a) GET b) POST
,Answer:
a (The browser sends an HTTP GET request the first time a script is loaded in the browser.)
Question:
Referring to the figure above, what is $_SERVER["REQUEST_METHOD"] when the user clicks
the "Guess" button a second time? a) GET b) POST
Answer:
b (Every time the user presses "Guess", the form posts back to the script, setting the request method
to POST.)
Question:
Where should the data validation code be placed? if ($_POST["num"] == "") { echo "<p>You didn't
type a number.</p>"; } a) Before if ($_SERVER["REQUEST_METHOD"] == "POST") {. } b)
Inside if ($_SERVER["REQUEST_METHOD"] == "POST") {. } c) After if
($_SERVER["REQUEST_METHOD"] == "POST") {. }
Answer:
b (Only when the request method is POST should $_POST["num"] be checked for a number.)
Question:
True or False? The regex /run/ matches the string "pruning".
Answer:
True (The characters "run" appear in consecutive order in "pruning".)
Question:
The regex /run/ matches the string "pr uning".
Answer:
False (The space between "r" and "un" causes the regex not to match)
,Question:
What regex matches the string "Regular Expression"? a) /Exp/ b) /exp/
Answer:
a (Regular expressions are case sensitive, so "Exp" matches the capital "E" in "Expression")
Question:
What regex matches the string "PHP"? a) /php/ b) /php/i
Answer:
b (The "i" makes the regular expressions case insensitive, so the regex matches "php" or "PHP")
Question:
What value is $x?$x = preg_match("/regex/", "regular expression"); a) 1 b) 0
Answer:
b (The characters "regex" do not appear in consecutive order in "regular expression")
Question:
What value is $x?$x = preg_match("/c\/1/", "abc/123"); a) 1 b) 0
Answer:
a (The regex matches "c/1". Because the forward slash character indicates the beginning and the
ending of a regex, the backslash character must precede the forward slash (like this: \/) to match a
forward slash)
Question:
What string does NOT match the regex /grea*t/? a) gret b) greaaat c) grat
Answer:
c (grat is missing an "e")
, Question:
What string does NOT match the regex /w+hy/? a) why b) hy c) wwwhy
Answer:
b (w+ means at least 1 "w" must exist)
Question:
What string does NOT match the regex /cat|bat|mat/? a) bbatt b) at c) mat
Answer:
b ("at" does not match because "at" is not preceded by c, b, or m)
Question:
What regex matches the string "boom"? a) /ba?oom/ b) /b\?oom/ c) /bo?m/
Answer:
a (a? means the "a" may be missing)
Question:
What regex matches the string "sleep like a baby"? a) /^like/ b) /like$/ c) /^sleep/
Answer:
c ("sleep" is at the beginning of the string.)
Question:
What regex matches the string "that's easy"? a) /b?eas$/ b) /b?sy$/ c) /e+sy$/
Answer:
b (b? matches a missing "b", and sy$ matches "sy" at the end of the string)
Question:
Question:
Referring to the figures above, if the user submitted the name as "<h1>Pam</h1>" and howmany as
4, the browser would display: "Pam ordered 4 widgets."
Answer:
False (htmlspecialchars() converts < into < and > into >, so the echo statement outputs
"<p><h1>Pam</h1> ordered 4 widgets.</p>". The browser displays < and > as <
and >, so the browser displays "<h1>Pam</h1> ordered 4 widgets.")
Question:
An attacker could use an XSS attack to redirect the user's browser to a malicious website.
Answer:
True (JavaScript in an XSS attack can redirect a browser to any URL.)
Question:
The following code uses the getData() function from the figure above and provides sufficient data
validation for a 16 digit credit card number. $creditCardNum = getData("creditCardNum"); if
(strlen($creditCardNum) == 16) { // Valid number } else { // Invalid number }
Answer:
False (The code does not ensure that $creditCardNum contains only digits, and $creditCardNum
could contain spaces between the numbers, so the length could be longer than 16 characters. A
regular expression (discussed elsewhere) is ideal for checking text against an expected pattern.)
Question:
What is $_SERVER["REQUEST_METHOD"] the first time a script is loaded in the web browser?
a) GET b) POST
,Answer:
a (The browser sends an HTTP GET request the first time a script is loaded in the browser.)
Question:
Referring to the figure above, what is $_SERVER["REQUEST_METHOD"] when the user clicks
the "Guess" button a second time? a) GET b) POST
Answer:
b (Every time the user presses "Guess", the form posts back to the script, setting the request method
to POST.)
Question:
Where should the data validation code be placed? if ($_POST["num"] == "") { echo "<p>You didn't
type a number.</p>"; } a) Before if ($_SERVER["REQUEST_METHOD"] == "POST") {. } b)
Inside if ($_SERVER["REQUEST_METHOD"] == "POST") {. } c) After if
($_SERVER["REQUEST_METHOD"] == "POST") {. }
Answer:
b (Only when the request method is POST should $_POST["num"] be checked for a number.)
Question:
True or False? The regex /run/ matches the string "pruning".
Answer:
True (The characters "run" appear in consecutive order in "pruning".)
Question:
The regex /run/ matches the string "pr uning".
Answer:
False (The space between "r" and "un" causes the regex not to match)
,Question:
What regex matches the string "Regular Expression"? a) /Exp/ b) /exp/
Answer:
a (Regular expressions are case sensitive, so "Exp" matches the capital "E" in "Expression")
Question:
What regex matches the string "PHP"? a) /php/ b) /php/i
Answer:
b (The "i" makes the regular expressions case insensitive, so the regex matches "php" or "PHP")
Question:
What value is $x?$x = preg_match("/regex/", "regular expression"); a) 1 b) 0
Answer:
b (The characters "regex" do not appear in consecutive order in "regular expression")
Question:
What value is $x?$x = preg_match("/c\/1/", "abc/123"); a) 1 b) 0
Answer:
a (The regex matches "c/1". Because the forward slash character indicates the beginning and the
ending of a regex, the backslash character must precede the forward slash (like this: \/) to match a
forward slash)
Question:
What string does NOT match the regex /grea*t/? a) gret b) greaaat c) grat
Answer:
c (grat is missing an "e")
, Question:
What string does NOT match the regex /w+hy/? a) why b) hy c) wwwhy
Answer:
b (w+ means at least 1 "w" must exist)
Question:
What string does NOT match the regex /cat|bat|mat/? a) bbatt b) at c) mat
Answer:
b ("at" does not match because "at" is not preceded by c, b, or m)
Question:
What regex matches the string "boom"? a) /ba?oom/ b) /b\?oom/ c) /bo?m/
Answer:
a (a? means the "a" may be missing)
Question:
What regex matches the string "sleep like a baby"? a) /^like/ b) /like$/ c) /^sleep/
Answer:
c ("sleep" is at the beginning of the string.)
Question:
What regex matches the string "that's easy"? a) /b?eas$/ b) /b?sy$/ c) /e+sy$/
Answer:
b (b? matches a missing "b", and sy$ matches "sy" at the end of the string)
Question: