1.Considering the code below, which of these statements are true?
<?php ini_set("allow_url_fopen", "1"); $page = file_get_contents("http://www.codepunker.com"); if($page!=FALSE) echo "Successfully fetched website contents"; else echo "An error has occurred";
The correct setting for loading web pages from a script is "allow_url_include"
The "allow_url_fopen" option can not be changed through ini_set. It can only be set in php.ini for security reasons
The if statement should be "if($page!==FALSE)" because "file_get_contents" can return non boolean values which evaluate to false
2.What is the output of the following script ?
<?php function generate() { for ($i = 1; $i <= 3; $i++) yield $i; } $generator = generate(); if(is_array($generator)) echo "Is Array"; elseif(is_object($generator)) echo "Is Object"; else echo "Is none of the above"; ?>
Is Array
Is Object
Is none of the above
3.When working with unfamiliar code, what is the best way to find out in which file a class is defined ?
Using ReflectionClass: <?php $reflection = new ReflectionClass('ClassName'); echo $reflection->getFileName(); ?>
<?php $reflection = new ReflectionClass('ClassName'); echo $reflection->getFileName(); ?>
Using the grep command to search through the application files: <?php $out = array(); exec("grep -r 'Classname' .", $out); var_dump($out); ?>
<?php $out = array(); exec("grep -r 'Classname' .", $out); var_dump($out); ?>
Using the get_declared_classes() function: $classes = get_declared_classes(); var_dump($classes);
$classes = get_declared_classes(); var_dump($classes);
4.Considering the code below ...
<?php class AppException extends Exception { function __toString() { return "Your code has just thrown an exception: {$this->message}\n"; } } class Students { public $first_name; public $last_name; public function __construct($first_name, $last_name) { if(empty($first_name)) { throw new AppException('First Name is required', 1); } if(empty($last_name)) { throw new AppException('Last Name is required', 2); } } } try { new Students('', ''); } catch (Exception $e) { echo $e; }
... which of these statements are correct ?
The catch (Exception $e) statement is wrong because it accepts an instance of the Exception class as the parameter. It should use an instance of the AppException class instead.
catch (Exception $e)
The __toString() method can't be overwritten in a child class because the methods in the Exception class are all final but for the constructor
__toString()
Exception
The magic __toString() method will be invoked automatically when the code enters the catch() statement and the custom exception message will be printed
catch()
5.Assuming that the code below is in a file named "test.php" and that PHP has full rights over the file, what happens if the file is executed from the command line without any arguments ?
exec("rm -f " . dirname(__FILE__) . "/" . $argv[0]);
The script exits without doing anything
The file test.php is deleted
A notice is thrown because $argv[0] is not defined and the script fails.
$argv[0]
6.When running PHP's built in FastCGI Process Manager (FPM) which of the following statements are true ?
Settings initialized in the php-fpm.conf file with php_admin_flag can not be overwritten in the code through ini_set()
php-fpm.conf
php_admin_flag
ini_set()
It doesn't provide any easy way of debugging slow performing requests/scripts
The special fastcgi_finish_request() function allows PHP to finish request and flush all data while continuing to do various tasks in the background.
fastcgi_finish_request()
7.Which of th following statements about object serialization are true ?
After serializing an object, a programmer can fully recreate that object in another file by calling unserialize() even if the class definition doesn't exist.
unserialize()
Static class properties can be serialized, but can not be unserialized.
Static class properties can not be serialized.
8.If you wanted a variable containing the letters A through Z, that allowed you to access each letter independently, which of the following approaches could you use?
If you wanted a variable containing the letters A through Z, that allowed you to access each letter independently, which of the following approaches could you use?
$str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
range('A', 'Z');
explode("", "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
You would use the ALPHA_ARRAY constant
None of the above
9.What is the output of the following code block? $array = array(1 => 0, 2, 3, 4); array_splice($array, 3, count($array), array_merge(array('x'), array_slice($array, 3))); var_dump($array);
What is the output of the following code block? $array = array(1 => 0, 2, 3, 4); array_splice($array, 3, count($array), array_merge(array('x'), array_slice($array, 3))); var_dump($array);
1 => 1, 2 => 2, 3 => x, 4 => 4
0 => 1, 2 => 2, 3 => 3, 4 => 4, x => 3
0 => 0, 1 => 2, 2 => 3, 3 => x, 4 => 4
0 => x, 1 => 0, 2 => 1, 3 => 2, 4 => 3
1 => 1, 3 => x, 2 => 2, 4 => 4
10.Which key will not be displayed from the following code block? $array = array('a' => 'John', 'b' => 'Coggeshall', 'c' => array('d' => 'John', 'e' => 'Smith')); function display($item, $key) { print "$key => $item\n"; } array_walk_recursive($array, "display");
Which key will not be displayed from the following code block? $array = array('a' => 'John', 'b' => 'Coggeshall', 'c' => array('d' => 'John', 'e' => 'Smith')); function display($item, $key) { print "$key => $item\n"; } array_walk_recursive($array, "display");
d
c
b
a
They all will be displayed
11.Given the following array: $array = array(1,1,2,3,4,4,5,6,6,6,6,3,2,2,2); The fastest way to determine the total number a particular value appears in the array is to use which function?
Given the following array: $array = array(1,1,2,3,4,4,5,6,6,6,6,3,2,2,2); The fastest way to determine the total number a particular value appears in the array is to use which function?
array_total_values
array_count_values
A foreach loop
count
a for loop
12.The following code snippet displays what for the resultant array? $a = array(1 => 0, 3 => 2, 4 => 6); $b = array(3 => 1, 4 => 3, 6 => 4); print_r(array_intersect($a, $b));
The following code snippet displays what for the resultant array? $a = array(1 => 0, 3 => 2, 4 => 6); $b = array(3 => 1, 4 => 3, 6 => 4); print_r(array_intersect($a, $b));
1 => 0
1 => 3, 3 => 1, 4 => 3
3 => 1, 3=> 2, 4 => 3, 4=> 6
1 => 0, 3 => 2, 4 => 6
An empty Array
13.What is the best way to iterate and modify every element of an array using PHP 5?
What is the best way to iterate and modify every element of an array using PHP 5?
You cannot modify an array during iteration.
for($i = 0; $i < count($array); $i++) { /* ... * / }
foreach($array as $key => &$val) { /* ... * / }
foreach($array as $key => $val) { /* ... * / }
while(list($key, $val) = each($array)) { /* ... * / }
14.Given a PHP value, which sample shows how to convert the value to JSON?
Given a PHP value, which sample shows how to convert the value to JSON?
$string = json_encode($value);
$string = Json::encode($value);
$json = new Json($value); $string = $json->__toString();
$value = (object) $value; $string = $value->__toJson();
15.Which one of the following XML declarations is NOT valid?
Which one of the following XML declarations is NOT valid?
<?xml version="1.0" ?>
<?xml version="1.1" encoding="UTF-8" ?>
<?xml standalone="no" ?>
<?xml standalone="1" ?>
16.What is the best way to ensure that a user-defined function is always passed an object as its single parameter?
What is the best way to ensure that a user-defined function is always passed an object as its single parameter?
function myfunction(stdClass $a)
function myfunction($a = stdClass)
Use is_object() within the function
There is no way to ensure the parameter will be an object.
function myfunction(Object $a)
17.What does the following function do, when passed two integer values for $p and $q? function magic($p, $q) { return ($q == 0) ? $p: magic($q, $p % $q); }
What does the following function do, when passed two integer values for $p and $q? function magic($p, $q) { return ($q == 0) ? $p: magic($q, $p % $q); }
Loops infinitely.
Switches the values of $p and $q.
Determines if they are both even or odd?
Determines the greatest common divisor between them.
Calculates the modulus between the two.
18.What is the output of the following? $a = 20; function myfunction($b) { $a = 30; global $a, $c; return $c = ($b + $a); } print myfunction(40) + $c;
What is the output of the following? $a = 20; function myfunction($b) { $a = 30; global $a, $c; return $c = ($b + $a); } print myfunction(40) + $c;
120
Syntax Error
60
70
19.What is the output of the following function? function &find_variable(&$one, &$two, &$three) { if($one > 10 && $one < 20) return $one; if($two > 10 && $two < 20) return $two; if($three > 10 && $three < 20) return $three; } $one = 2; $two = 20; $three = 15; $var = &find_variable($one, $two, $three); $var++; print "1: $one, 2: $two, 3: $three";
What is the output of the following function? function &find_variable(&$one, &$two, &$three) { if($one > 10 && $one < 20) return $one; if($two > 10 && $two < 20) return $two; if($three > 10 && $three < 20) return $three; } $one = 2; $two = 20; $three = 15; $var = &find_variable($one, $two, $three); $var++; print "1: $one, 2: $two, 3: $three";
1: 2, 2: 20, 3: 15
1: 3, 2: 21, 3: 16
1: 2, 2: 21, 3: 15
1: 3, 2: 20, 3: 15
1: 2, 2: 20, 3: 16
20.In PHP 5 you can use the ______ operator to ensure that an object is of a particular type. You can also use _______ in the function declaration.
In PHP 5 you can use the ______ operator to ensure that an object is of a particular type. You can also use _______ in the function declaration.
instanceof, is_a
instanceof, type-hinting
type, instanceof
===, type-hinting
===, is_a
21.Which php.ini directive should be disabled to prevent the execution of a remote PHP script via an include or require construct?
Which php.ini directive should be disabled to prevent the execution of a remote PHP script via an include or require construct?
You cannot disable remote PHP script execution
curl.enabled
allow_remote_url
allow_url_fopen
allow_require
22. Which of the following list of potential data sources should be considered trusted?
Which of the following list of potential data sources should be considered trusted?
$_ENV
$_GET
$__COOKIE
$_SERVER
23.Consider the following code: 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) } /* .... */ What potential security hole would this code snippet produce?
Consider the following code: 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) } /* .... */ What potential security hole would this code snippet produce?
Cross-Site Scripting Attack
There is no security hole in this code
Code Injection
SQL Injection
Cross-Site Request Forgery
24.Given the two values below, which of the following possibilities will print 10 foos20 bars? $var1 = "10 foos"; $var2 = "20 bars"; print ???????;
Given the two values below, which of the following possibilities will print 10 foos20 bars? $var1 = "10 foos"; $var2 = "20 bars"; print ???????;
implode("", array($var1,$var2));
$var1 . $var2
$var1 + $var2
All of the above
25.What is the output of the following code? $string = "14302"; $string[$string[2]] = "4"; print $string;
What is the output of the following code? $string = "14302"; $string[$string[2]] = "4"; print $string;
14304
14342
44302
14402
Array
26.Which PCRE regular expression will match the string PhP5-rocks
Which PCRE regular expression will match the string PhP5-rocks
/^[hp1-5]*\-.*/i
/[hp1-5]*\-.?/
/[hp][1-5]*\-.*/
/[PhP]{3}[1-5]{2,3}\-.*$/
/[a-z1-5\-]*/
27.If regular expressions must be used, in general which type of regular expression functions available to PHP is preferred for performance reasons?
If regular expressions must be used, in general which type of regular expression functions available to PHP is preferred for performance reasons?
strtok() using regular expressions
preg_* regular expression functions
parse_str() using regular expressions
strregex* regular expression functions
ereg* regular expression functions
28.To destroy one variable within a PHP session you should use which method in PHP 5?
To destroy one variable within a PHP session you should use which method in PHP 5?
Unset the variable in $HTTP_SESSION_VARS
Use the session_destroy() function
Use the session_unset() function
unset the variable in $_SESSION using unset()
Any of the above are acceptable in PHP 5
29.If you would like to change the session ID generation function, which of the following is the best approach for PHP 5?
If you would like to change the session ID generation function, which of the following is the best approach for PHP 5?
Set the session.hash_function INI configuration directive
Use the session_set_id_generator() function
Set the session id by force using the session_id() function
Use the session_regenerate_id() function
Implement a custom session handler
30.Which of the following is not a valid fopen() access mode:
Which of the following is not a valid fopen() access mode:
x
w
r+