Test Exam 1
Test Exam 1
Test Exam 1
<?php $a = "The quick brown fox jumped over the lazy dog."; $b = array_map("strtoupper", explode(" ", $a)); foreach($b as $value) { print "$value "; } ?>
4. Whatistheoutputofthefollowingcode?
<?php function x10(&$number) $number *= 10; $count = 5; x10($count); echo $count; ?>
<?php $oranges = 10; $apples = 5; $string = "I have %d apples and %d oranges"; ??????? ?>
Whatcouldbeplacedinplaceof??????tooutputthestring: I have 5 apples and 10 oranges Answers:(choose2) str_format($string,$apples,$oranges); print($string,$apples,$oranges); printf($string,$apples,$oranges); printsprintf($apples,$oranges); sprintf($string,$oranges,$apples);
9. Howcanonetakeadvantageofthetimewaitingforalockduringastreamaccess,todoothertasks usingthefollowinglockingcodeasthebase: $retval = flock($fr, LOCK_EX); Answer... Useflock_lazy()insteadofflock() UseLOCK_EX|LOCK_NBinsteadofLOCK_EX UseLOCK_UNinsteadofLOCK_EX Checkthevalueof$retvaltoseeifthelockwasobtained Checktoseeif$retval==LOCK_WAIT 10. Whichofthefollowingfunctionswilltrimleadingand/ortrailingwhitespacefromastring? Answers:(choose3) ltrim() rtrim() wtrim() trim() str_replace() 11. Whichofthefollowingfunctionsareusedwiththeinternalarraypointertoaccomplishanaction? Answers:(choose4) key forward prev current next
Answer... Writeaparsercompletelybyhand,it'stheonlywaytomakesureit's100%accurate Usetheparse_str()functiontotranslateittoanarray() PassthevariabletoanotherPHPscriptviaanHTTPGETrequestandreturnthearrayasa serializedvariable Justcallunserialize()totranslateittoanarray() Writeastringparserusingstrtok()andunserialize()toconvertittoanarray 14. ImplementingyourownPDOclassrequireswhichstepsfromthelistbelow? Answers:(choose3) ExtendingthePDOStatementClass SetthePDO::ATTR_STATEMENT_CLASSparameter CallthePDO::setStatementClass()method ExtendthePDOclass SetthePDO::ATTR_USE_CLASSparamater
15. Whatiswrongwiththefollowingcodesnippet?Assumedefaultconfigurationvaluesapply.
<?php $fp = fsockopen('www.php.net', 80); fwrite($fp, "GET / HTTP/1.0\r\nHost: www.php.net\r\n"); $data = fread($fp, 8192); ?>
16. Whatistheoutputofthefollowingcode?
<?php function functionSplit() { $pre = 1; ?> <?php echo $pre; } functionSplit(); ?>
17. GiventhefollowingXMLdocumentinaSimpleXMLobject:
<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>XML Example</title> </head> <body> <p> Moved to <<a href="http://example.org/">http://www.example.org/</a>.> <br/>
22. Considerthefollowingscript:
<?php function func(&$arraykey) { return $arraykey; // function returns by value! } $array = array('a', 'b', 'c'); foreach (array_keys($array) as $key) { $y = &func($array[$key]); $z[] =& $y; } var_dump($z); ?>
24. Considerthefollowingcodesnippet:
<?php $query = "INSERT INTO mytable (myinteger, mydouble, myblob, myvarchar) VALUES (?, ?, ?, ?)"; $statement = mysqli_prepare($link, $query); if(!$statement) { die(mysqli_error($link)); } /* The variables being bound to by MySQLi don't need to exist prior to binding */ mysqli_bind_param($statement, "idbs", $myinteger, $mydouble, $myblob, $myvarchar); /* ???????????? */ /* execute the query, using the variables as defined. */ if(!mysqli_execute($statement)) { die(mysqli_error($link)); } ?>
26. Whatistheoutputofthefollowingcode?
Answer... Nooutputorerror.Variablescannotbeoptionalandpassedbyreference.
5 6
28. Whatshouldgointhemissingline?????belowtoproducetheoutputshown?
<?php $array_one = array(1,2,3,4,5); $array_two = array('A', 'B', 'C', 'D', 'E'); ??????? print_r($array_three); ?>
Result:
Array ( [5] [4] [3] [2] [1] ) => => => => => A B C D E
31. Whatistheoutputofthefollowingcode?
<?php class MyException extends Exception {} class AnotherException extends MyException {} class Foo { public function something() { throw new AnotherException(); } public function somethingElse() { throw new MyException(); } } $a = new Foo(); try { try { $a->something(); } catch(AnotherException $e) { $a->somethingElse(); } catch(MyException $e) { print "Caught Exception"; } } catch(Exception $e) { print "Didn't catch the Exception!"; } ?>
"Didn'tcatchtheException!"followedbyafatalerror "CaughtException"
32. Considerthefollowingscript:
<?php $dom = new DOMDOcument(); $dom->load("myxmlfile.xml"); foreach($dom->documentElement->childNodes as $child) { if(($child->nodeType == XML_ELEMENT_NODE) && $child->nodeName == "item") { foreach($child->childNodes as $item) { if(($item->nodeType == XML_ELEMENT_NODE) && ($item->nodeName == "title")) { print "$item->firstChild->data\n"; } } } } ?>
get_cookie('mycookie') Noneoftheabove
40. Whatistheoutputofthefollowing?
<?php function byRef(&$apples) { $apples++; } $oranges = 5; $apples = 5; byRef($oranges); echo "I have $apples apples and $oranges oranges"; ?>
Filteralloutput Filterallinput
42. Considerthefollowingcode:
<?php session_start(); if(!empty($_REQUEST['id']) && !empty($_REQUEST['quantity'])) { $id = scrub_id($_REQUEST['id']); $quantity = scrub_quantity($_REQUEST['quantity']) $_SESSION['cart'][] = array('id' => $id, 'quantity' => $quantity) } /* .... */ ?>
43. InPHP5objectsarepassedbyreferencetoafunctionwhen(Selecttheanswerthatisthemostcorrect): Answer... Always;objectsarepassedbyreferenceinPHP5 Whenthecallingcodepreceedsthevariablenamewitha& Never;objectsareclonedwhenpassedtoafunction Whenthefunctionparamaterlistingpreceedsthevariablenamewitha& 44. UnliketheoldMySQLextension,thenewMySQLiextensionrequiresthatyouprovidewhatwhen performingaquerywhenusingtheproceduralinterface? Answer... Thequeryidentifier Thedatabasename Allfunctionparameters Thedatabasehandle Thestatementhandle
47. Considerthefollowingcodesegment:
<?php $xmldata = <<< XML <?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>XML Example</title> </head> <body> <p> Moved to <<a href="http://example.org/">http://www.example.org/</a>.> <br/> </p> </body> </html> XML; $xml = xml_parser_create("UTF-8"); /* ??????? */ xml_parse($xml, $xmldata); function xml_start_handler($xml, $tag, $attributes) { print "Tag: $tag<br/>\n"; } function xml_end_handler($xml, $tag) { } ?>
48. Whatistheoutputofthefollowing?
<?php $a = 010; $b = 0xA; $c = 2; print $a + $b + $c; ?>
Answer... 20 22 18 $aisaninvalidvalue 2
50. WhichofthefollowingfunctionsarepartofPHP'sinternalIteratorinterface? Answers:(choose5) rewind() valid() next() key() current() 51. WhenmigratingthefollowingcodefromPHP4toPHP5,whatshouldbechanged?
<?php class MyClass {
function MyClass($param) { /* Do something with $param */ $this->_doSomething($param); } // Private method to MyClass function _doSomething($param) { /* Do something with $param */ }
} ?>
Parsethe$_SERVER['REQUEST_URI']variable
54. Whatistheoutputofthefollowingscript?
<?php class ClassOne { protected $a = 10; public function changeValue($b) { $this->a = $b; } } class ClassTwo extends ClassOne { protected $b = 10; public function changeValue($b) { $this->b = 10; parent::changeValue($this->a + $this->b); } public function displayValues() { print "a: {$this->a}, b: {$this->b}\n"; } } $obj = new ClassTwo(); $obj->changeValue(20); $obj->changeValue(10); $obj->displayValues(); ?>
functionb($a=(10+2))
56. ConsiderthefollowingPHPscript:
<?php function get_socket($host, $port) { $fr = fsockopen($host, $port); stream_set_blocking($fr, false); return $fr; } // Assume $host1, $host2, etc are defined properly $write_map[] = array('fr' => get_socket($host1, $port1), 'data' => str_pad("", 500000, "A")); $write_map[] = array('fr' => get_socket($host2, $port2), 'data' => str_pad("", 500000, "B")); $write_map[] = array('fr' => get_socket($host3, $port3), 'data' => str_pad("", 500000, "C")); do { $write_sockets = array(); foreach($write_map as $data) { $write_sockets[] = $data['fr']; } $num_returned = stream_select($r = null, $write_sockets, $e = null, 30); if($num_returned) { foreach($write_sockets as $fr) { foreach($write_map as $index => $data) { if($data['fr'] === $fr) { $len = fwrite($fr, $data['buf']); if($len) { $data['buf'] = substr($data['buf'], $len); if(empty($data['buf'])) { fclose($data['fr']); /* ????????? */ } } } } } } } while(count($write_map)); ?>
Whatshouldgointhe???????aboveforthisscripttofunctionproperly? 57. WhichtwointernalPHPinterfacesprovidefunctionalitywhichallowyoutotreatanobjectlikean array? Answers:(choose2) iteration arrayaccess objectarray iterator array
62. ConsiderthefollowingexampleXMLdocument:
<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>XML Example</title> </head> <body> <p> Moved to <<a href="http://example.org/">http://www.example.org/</a>.> <br> </p> </body> </html>
66. Whatiswrongwiththefollowingcode?
<?php function duplicate($obj) { $newObj = $obj; return $newObj; } $a = new MyClass(); $a_copy = duplicate($a); $a->setValue(10); $a_copy->setValue(20); ?>