1.What happens when the code below is executed ?
<?php class foo { private $variable; function __construct() { $this->variable = 1; } function __get($name) { return $this->$name; } } $a = new foo; echo $a->variable; ?>
The script outputs 1
Fatal error: Cannot access private property foo::$variable
The script outputs 0
2.What happens when the script below is executed ?
<?php namespace CustomArea; error_reporting(E_ALL); ini_set("display_errors", "on"); function var_dump($a) { return str_replace("Weird", $a, "Weird stuff can happen"); } $a = "In programming"; echo var_dump($a); ?>
PHP Fatal error: Cannot redeclare var_dump()
Weird stuff can happen
In programming stuff can happen
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.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.
5.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
6.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
7.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
8.When dealing with cloned objects in PHP, which of the following statements are true ?
The programmer can make use of the __clone() magic method to stop an object from being cloned.
__clone()
As of PHP 7.0.0, members of cloned objects can be accessed in a single expression without any assignments... Like this: (clone new DateTime())->format('Y');
(clone new DateTime())->format('Y');
The __clone() magic method of a class is called before the actual cloning of the object occurs allowing the programmer to alter values before the cloning process begins.
9.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
10.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
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.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;
13.The ____ construct is particularly useful to assign your own variable names to values within an array.
The ____ construct is particularly useful to assign your own variable names to values within an array.
array_get_variables
current
each
import_variables
list
14.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" ?>
15.Which of the following will NOT instantiate a DateTime object with the current timestamp?
Which of the following will NOT instantiate a DateTime object with the current timestamp?
$date = new DateTime();
$date = new DateTime('@' . time());
$date = new DateTime('now');
$date = new DateTime(time());
16.Transactions are used to...
Transactions are used to...
guarantee high performance
secure data consistency
secure access to the database
reduce the database server overhead
reduce code size in PHP
17.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();
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.How does one access standard input/output and error streams in PHP 5?
How does one access standard input/output and error streams in PHP 5?
STDIN, STDOUT and STDERR constants
Use stdin(), stdout() and stderr() functions
PHP::STDIN, PHP::STDOUT and PHP::STDERR class constants
Use PHP::stdin(), PHP::stdout() and PHP::stderr() class functions
21.How to access standard error stream in PHP ?
How to access standard error stream in PHP ?
$stderr = STDERR;
$stderr = fopen("php://stderr", "w");
$stderr = stderr("w")
$stderr = fwrite("php://stderr");
22.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
23.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
24.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.
final
protected
incomplete
abstract
implements
25.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
26.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
27.If you would like to store your session in the database, you would do which of the following?
If you would like to store your session in the database, you would do which of the following?
It requires a custom PHP extension to change the session handler
Implement the session_set_save_handler() function
Create functions for each session handling step and use session_set_save_handler() to override PHP's internal settings
Configure the session.save_handler INI directive to your session class
28.Consider the following HTML fragment: <select name="???????" multiple> <option value="1">Item #1</option> <!-- ... more options ... --> </select> Which of the following name attributes should be used to capture all of the data from the user in PHP?
Consider the following HTML fragment: <select name="???????" multiple> <option value="1">Item #1</option> <!-- ... more options ... --> </select> Which of the following name attributes should be used to capture all of the data from the user in PHP?
myselectbox=array()
myselectbox[]
myselectbox['multiple']
myselectbox{'multiple'}
myselectbox
29.Consider the following function: function redirect($url) { // Check to make sure we haven't already sent // the header: if(/*???????*/) { header("Location: $url"); } } What conditional should replace the ????? above?
Consider the following function: function redirect($url) { // Check to make sure we haven't already sent // the header: if(/*???????*/) { header("Location: $url"); } } What conditional should replace the ????? above?
!in_array("Location: $url", headers_list())
!header_exists("Location: $url")
!header_location($url)
$_SERVER['HTTP_LOCATION'] != $url
30.Which of the following is not a valid fopen() access mode:
Which of the following is not a valid fopen() access mode:
b
x
a
w
r+