1.What is the output of the code below ?
$now = new DateTime(); $now2 = new DateTime(); $ago = new DateInterval('P4Y10M3W'); $ago2 = new DateInterval('P4Y10M2W7D'); $then = $now->sub($ago); $date1 = $then->format('Y-m-d'); $then2 = $now2->sub($ago2); $date2 = $then2->format('Y-m-d'); var_dump ($date1 === $date2);
bool(false) - because the '2W' part in $ago2 will get overwritten by the '7D' part and therefor the second date interval will be 2 Weeks shorter than the first interval.
bool(false)
$ago2
bool(true) - because the two interval definitions are equivalent.
bool(true)
bool(false) and the script will throw a notice because the date/time interval notation in the $ago2 variable is wrong.
2.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()
3.What is the output of the code below ?
<?php namespace animals; ini_set('error_reporting', E_ALL); ini_set('display_errors', 'on'); class Cat { static function Definition() { return 'Cats are ' . __NAMESPACE__; } } namespace animals\pets; class Cat { static function Definition() { return 'Cats are ' . __NAMESPACE__; } } echo Cat::Definition();
Cats are animals\pets
Fatal error: Cannot redeclare class Cat
Fatal error: Cannot re-declare class animals\pets\Cat in sub-namespace
4.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()
5.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.
6.What happens if you execute the code below ?
<?php class someclass { public $someprop; function __construct() { $this->someprop = 1; } } function somefunc(&$instance) { unset($instance); } $instance = new someclass; somefunc($instance); var_dump($instance);
NULL
object(someclass)#1 (1) { ["someprop"]=> int(1) }
Warning (Only variables can be passed by refence) NULL
7.Which of the following functions will sort an array in ascending order by value, while preserving key associations?
Which of the following functions will sort an array in ascending order by value, while preserving key associations?
asort()
usort()
krsort()
ksort()
sort()
8.What is the output of this code snippet? $a = array(0.001 => 'b', .1 => 'c'); var_dump($a);
What is the output of this code snippet? $a = array(0.001 => 'b', .1 => 'c'); var_dump($a);
An empty array
0.001 => 'b', .1 => c
0 => 'c'
'0.001' => 'b', '0.1' => c'
A Syntax Error
9.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
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.What is the result of the following code snippet? $array = array('a' => 'John', 'b' => 'Coggeshall', 'c' => array('d' => 'John', 'e' => 'Smith')); function something($array) { extract($array); return $c['e']; } print something($array);
What is the result of the following code snippet? $array = array('a' => 'John', 'b' => 'Coggeshall', 'c' => array('d' => 'John', 'e' => 'Smith')); function something($array) { extract($array); return $c['e']; } print something($array);
Smith
A PHP Warning
Coggeshall
Array
12.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
13.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
14.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)) { /* ... * / }
15.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)
16.What would go in place of ?????? below to make this script execute without a fatal error? $a = 1; $b = 0; /* ?????? */ $c = $a / $b;
What would go in place of ?????? below to make this script execute without a fatal error? $a = 1; $b = 0; /* ?????? */ $c = $a / $b;
quit();
die();
stop();
__halt_compiler();
exit();
17.What would you replace ??????? with, below, to make the string Hello, World! be displayed? function myfunction() { /* ??????? */ print $string; } myfunction("Hello, World!");
What would you replace ??????? with, below, to make the string Hello, World! be displayed? function myfunction() { /* ??????? */ print $string; } myfunction("Hello, World!");
There is no way to do this.
$string = $argv[1];
$string = $_ARGV[0];
list($string) = func_get_args();
$string = get_function_args();
18.What are the three access modifiers that you can use in PHP objects?
What are the three access modifiers that you can use in PHP objects?
protected
public
static
private
final
19.When checking to see if two variables contain the same instance of an object, which of the following comparisons should be used?
When checking to see if two variables contain the same instance of an object, which of the following comparisons should be used?
if($obj1->equals($obj2) && ($obj1 instanceof $obj2))
if($obj1->equals($obj2))
if($obj1 instanceof $obj2)
if($obj1 === $obj2)
20.The ______ keyword is used to indicate an incomplete class or method, which must be further extended and/or implemented in order to be used.
The ______ keyword is used to indicate an incomplete class or method, which must be further extended and/or implemented in order to be used.
incomplete
abstract
implements
21.Type-hinting and the instanceof keyword can be used to check what types of things about variables?
Type-hinting and the instanceof keyword can be used to check what types of things about variables?
If a particular child class extends from it
If they are an instance of a particular interface
If they are an abstract class
If they have a particular parent class
If they are an instance of a particular class
22.What three special methods can be used to perform special logic in the event a particular accessed method or member variable is not found?
What three special methods can be used to perform special logic in the event a particular accessed method or member variable is not found?
__get($variable)
__call($method, $params)
__destruct()
__set($variable, $value)
__call($method)
23.What is the best measure one can take to prevent a cross-site request forgery?
What is the best measure one can take to prevent a cross-site request forgery?
Disallow requests from outside hosts
Add a secret token to all form submissions
Turn off allow_url_fopen in php.ini
Filter all output
Filter all input
24.What variable reference would go in the spots indicated by ????? in the code segment below? $msg = "The Quick Brown Foxed Jumped Over the Lazy Dog"; $state = true; $retval = ""; for ($i = 0; (isset(??????)); $i++) { if($state) { $retval .= strtolower(?????); } else { $retval .= strtoupper(?????); } $state = !$state; } print $retval;
What variable reference would go in the spots indicated by ????? in the code segment below? $msg = "The Quick Brown Foxed Jumped Over the Lazy Dog"; $state = true; $retval = ""; for ($i = 0; (isset(??????)); $i++) { if($state) { $retval .= strtolower(?????); } else { $retval .= strtoupper(?????); } $state = !$state; } print $retval;
$msg{$i}
ord($msg);
chr($msg);
substr($msg, $i, 2);
25.Which of the following is the best way to split a string on the "-=-" pattern?
Which of the following is the best way to split a string on the "-=-" pattern?
They all are equally proper methods
str_split($string, strpos($string, "-=-"))
preg_split("-=-", $string);
explode("-=-", $string);
26.Which function is best suited for removing markup tags from a string?
Which function is best suited for removing markup tags from a string?
strip_markup
strip_tags
str_replace
preg_replace
preg_strip
27.Consider the following script: $oranges = 10; $apples = 5; $string = "I have %d apples and %d oranges"; ??????? What could be placed in place of ?????? to output the string: "I have 5 apples and 10 oranges"
Consider the following script: $oranges = 10; $apples = 5; $string = "I have %d apples and %d oranges"; ??????? What could be placed in place of ?????? to output the string: "I have 5 apples and 10 oranges"
str_format($string, $apples, $oranges);
print($string, $apples, $oranges);
printf($string, $apples, $oranges);
print sprintf($apples, $oranges);
sprintf($string, $oranges, $apples);
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.Setting a HTTP cookie on the client which is not URL-encoded is done how in PHP 5?
Setting a HTTP cookie on the client which is not URL-encoded is done how in PHP 5?
Use the setrawcookie() function
Set the cookies.urlencode INI directive to false
Use urldecode() on the return value of setcookie()
Setting the $no_encode parameter of setcookie() to a boolean 'true'
All cookies must be URL encoded
30.One can ensure that headers can always be sent from a PHP script by doing what?
One can ensure that headers can always be sent from a PHP script by doing what?
Enable header buffering in PHP 5
Set the header.force INI directive to true
Enable output buffering in PHP 5
There is no way to ensure that headers can always be set, they must always be checked