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 $a = array(); $a[0] = 1; unset($a[0]); echo ($a != null) ? 'True' : 'False';
True
False
Parse Error
4.Which of the statements about traits below are true ?
traits
PHP is "single inheritance" so traits allow coders to reuse sets of methods from the trait freely in several independent classes
An inherited member from a base class is overridden by a member inserted by a Trait, but members from the current class override Trait methods.
If two Traits insert a method with the same name in a class that uses them, a fatal error is produced and this naming conflict can not be bypassed
5.What is the best way to store and verify passwords in PHP ?
Using a hashing algorithm like md5 or sha256 and then, when verifying compare the stored hash with the hash of the string submitted by the user
md5
sha256
Using the built in password_create() and password_verify() functions in PHP
password_create()
password_verify()
Using the strongest to date hashing algorithm combined with a salt to avoid dictionary attacks.
salt
6.What is the output of the following PHP script?
<?php $a = 1; $b = 2.5; $c = 0xFF; $d = $b + $c; $e = $d * $b; $f = ($d + $e) % $a; print ($f + $e);
643.75
432
643
257
432.75
7.What is the output of the following code block? $a = "The quick brown fox jumped over the lazy dog."; $b = array_map("strtoupper", explode(" ", $a)); foreach ($b as $value) { print "$value "; }
What is the output of the following code block? $a = "The quick brown fox jumped over the lazy dog."; $b = array_map("strtoupper", explode(" ", $a)); foreach ($b as $value) { print "$value "; }
THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG.
A PHP Error
Array Array Array Array Array Array Array Array Array
8.Which of the following functions could be used to break a string into an array?
Which of the following functions could be used to break a string into an array?
array_split()
split()
string_split()
preg_match_all()
explode()
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.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
NULL
Array
11.What should go in the missing line ????? below to produce the output shown? $array_one = array(1,2,3,4,5); $array_two = array('A', 'B', 'C', 'D', 'E'); ??????? print_r($array_three); Result: Array ( [5] => A [4] => B [3] => C [2] => D [1] => E )
What should go in the missing line ????? below to produce the output shown? $array_one = array(1,2,3,4,5); $array_two = array('A', 'B', 'C', 'D', 'E'); ??????? print_r($array_three); Result: Array ( [5] => A [4] => B [3] => C [2] => D [1] => E )
$array_three = array_merge(array_reverse($array_one), $array_two);
$array_three = array_combine($array_one, $array_two);
$array_three = array_combine(array_reverse($array_one), $array_two);
$array_three = array_merge($array_one, $array_two);
$array_three = array_reverse($array_one) + $array_two;
12.Which of the following tags are an acceptable way to begin a PHP Code block?
Which of the following tags are an acceptable way to begin a PHP Code block?
<SCRIPT LANGUAGE="php">
<!
<%
<?
13.What is the output of the following PHP code? define("FOO", 10); $array = [10 => FOO,"FOO" => 20]; print $array[$array[FOO]] * $array["FOO"];
What is the output of the following PHP code? define("FOO", 10); $array = [10 => FOO,"FOO" => 20]; print $array[$array[FOO]] * $array["FOO"];
FOO
100
200
20
10
14.What is the output of the following PHP script? $a = 1; $b = 2.5; $c = 0xFF; $d = $b + $c; $e = $d * $b; $f = ($d + $e) % $a; print ($f + $e);
What is the output of the following PHP script? $a = 1; $b = 2.5; $c = 0xFF; $d = $b + $c; $e = $d * $b; $f = ($d + $e) % $a; print ($f + $e);
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.How can you modify the copy of an object during a clone operation?
How can you modify the copy of an object during a clone operation?
Put the logic in the object's constructor to alter the values
Implment your own function to do object copying
Implement the object's __clone() method
Implement __get() and __set() methods with the correct logic
Implement the __copy() method with the correct logic
18.What is the output of the following script? 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();
What is the output of the following script? 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();
a: 30, b: 30
a: 30, b: 20
a: 30, b: 10
a: 20, b: 20
a: 10, b: 10
19.Which of the following php.ini directives should be disabled to improve the outward security of your application?
Which of the following php.ini directives should be disabled to improve the outward security of your application?
safe_mode
magic_quotes_gpc
register_globals
display_errors
allow_url_fopen
20.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
21.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
22.Which of the following are not valid ways to embed a variable into a string?
Which of the following are not valid ways to embed a variable into a string?
$a = "Value: $value->getValue()";
$a = "Value: {$value}";
$a = 'Value: $value';
$a = "Value: $value";
$a = "Value: {$value['val']}";
23.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);
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.When comparing two strings, which of the following is acceptable?
When comparing two strings, which of the following is acceptable?
$a === $b;
strcasecmp($a, $b);
strcmp($a, $b);
$a == $b;
str_compare($a, $b);
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.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
28.When uploading a file using HTTP, which variable can be used to locate the file on PHP's local filesystem?
When uploading a file using HTTP, which variable can be used to locate the file on PHP's local filesystem?
$_FILES['fieldname']['tmp_name']
$_FILES['fieldname']
$_FILES['fieldname'][0]['filename']
$_FILES['fieldname']['filename']
29.Setting a cookie on the client in PHP 5 can be best accomplished by:
Setting a cookie on the client in PHP 5 can be best accomplished by:
Use the add_cookie() function
Use the setcookie() function
Use the the apache_send_header() function
Setting a variable in the $_COOKIE superglobal
30.How does one create a cookie which will exist only until the browser session is terminated?
How does one create a cookie which will exist only until the browser session is terminated?
You cannot create cookies that expire when the browser session is terminated
Setting the expiration time for a cookie to a time in the distant future
Do not provide a cookie expiration time
Enable Cookie Security
Set a cookie without a domain