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.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
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.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()
6.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
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 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
10.Which from the following list is not an appropriate use of an array?
Which from the following list is not an appropriate use of an array?
As a list
All of these uses are valid
As a Lookup Table
A Stack
As a hash table
11.Which function would you use to add an element to the beginning of an array?
Which function would you use to add an element to the beginning of an array?
array_shift()
array_push()
$array[0] = "value";
array_unshift()
array_pop()
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.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">
<!
<%
<?
14.Which of the following are valid PHP variables?
Which of the following are valid PHP variables?
@$foo
&$variable
${0x0}
$0x0
15.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
16.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);
643.75
432
643
257
432.75
17.What is the output of the following? $a = 010; $b = 0xA; $c = 2; print $a + $b + $c;
What is the output of the following? $a = 010; $b = 0xA; $c = 2; print $a + $b + $c;
22
$a is an invalid value
2
18.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());
19.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();
20.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
21.To ensure that a given object has a particular set of methods, you must provide a method list in the form of an ________ and then attach it as part of your class using the ________ keyword.
To ensure that a given object has a particular set of methods, you must provide a method list in the form of an ________ and then attach it as part of your class using the ________ keyword.
array, interface
interface, implements
interface, extends
instance, implements
access-list, instance
22.The _______ method will be called automatically when an object is represented as a string.
The _______ method will be called automatically when an object is represented as a string.
getString()
__get()
__value()
__getString()
23.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
24.Consider the following code: header("Location: {$_GET['url']}\"); Which of the following values of $_GET['url'] would cause session fixation?
Consider the following code: header("Location: {$_GET['url']}\"); Which of the following values of $_GET['url'] would cause session fixation?
Session Fixation is not possible with this code snippet
http://www.zend.com/?PHPSESSID=123
PHPSESSID%611243
Set-Cookie%3A+PHPSESSID%611234
http%3A%2F%2Fwww.zend.com%2F%0D%0ASet-Cookie%3A+PHPSESSID%611234
25.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);
26.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);
27.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);
28.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);
29.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
30.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