Brainbench php 5.3 Question Answers
1.Based on the sample code below, what is the result ?
<?php
class Vehicle {
protected static $mCarColor = 'White';
protected static $mCarDoors = 4;
public static function getCarColor() {
return self::$mCarColor;
}
public static function getCarDoors() {
return self::$mCarDoors;
}
}
class Cabriolet extends Vehicle {
protected static $mCarColor = 'Red';
protected static $mCarDoors = 3;
public static function getCarDoors() {
return self::$mCarDoors;
}
}
echo Vehicle::getCarColor()." ".Vehicle::getCarDoors()." <br>". Cabriolet::getCarColor()." ".Cabriolet::getCarDoors();
?>
Answer:
White 4
White 3
White 3
2.There are 10 input elemtes named MyInput in DOM. You need to select the third third input elements using xpath
<?php
$vstr ="<Tags>";
$vstr .="<MyInput>1</MyInput>";
$vstr .="<MyInput>2</MyInput>";
$vstr .="<MyInput>3</MyInput>";
$vstr .="<MyInput>4</MyInput>";
$vstr .="<MyInput>5</MyInput>";
$vstr .="<MyInput>6</MyInput>";
$vstr .="<MyInput>7</MyInput>";
$vstr .="<MyInput>8</MyInput>";
$vstr .="<MyInput>9</MyInput>";
$vstr .="<MyInput>10</MyInput>";
$vstr .="</Tags>";
$xml = new SimpleXMLElement($vstr);
$result = $xml->xpath('/Tags');
print "<pre>";
print_r($result);
?>
Answer
input[3][@name="MyInput"]
3.Which line of code do you use to place a variable in a valid URL?
Answer:
$url = "http://yoursite.com/test.php?var=$Variable";
4.Based on the sample code below, what is the result
<?php
$str1 = "Y-m-";
$str2 = " Sj";
$str3 = "\i\s \\t\h\e l";
$dt1 = new DateTime("20080203 13:14:15");
$dt1->modify("+3 d");
$dt1->modify("+2 m");
$dt1->modify("+1 year");
date_sub($dt1, new DateInterval("P2M"));
print "<br>";
echo $dt1->format($str1.strrev($str2).$str3);
?>
Answer:
2008-12-3rd is the Wednesday
5.which function return natural log value ?
Answer:
log()
6.Based on the sample code below, what is the result ?
<?php
class Fruit
{
protected $mType = '';
protected $mColor = 'Green';
public function getType()
{ return $this->mType;
}
final public function getColor()
{
return $this->mColor;
}
}
final class Apple extends Fruit {
protected $mType = '';
protected $mColor = 'Red';
final public function getColor()
{
return "Apple:".$this->mColor;
}
}
$generalFruit = new Fruit();
$myApple = new Apple();
echo $generalFruit->getType().":".$generalFruit->getColor(). " ".
$myApple->getType().":".$myApple->getColor();
?>
Answer:
Fatal error: Cannot override final method Fruit::getColor()
7.Based on the sample code below, what is the result ?
<?php
$f = file("MyFile");
$num = count($f);
$o = fopen("MyNewFile","w");
for($i = 0; $i < $num; $i++)
{
$l = strlen($f[$i]);
$halfstr = substr($f[$i], 0, $l/2);
fputs($o, $halfstr."\n", 1024);
}
fclose($o);
?>
Answer:
The first half of line moved to MyNewFile file
8.To get folowing output apple,banana,cherry, orange,which line below replace *.
<?php
$fruit_list = array("a"=>"apple", "b"=>"banana", "c"=>"cherry", "d"=>"orange");
while (count($fruit_list))
{
*
echo $fruit . ", ";
}
?>
Answer:
$fruit= array_shift($fruit_list);
9.Based on the sample code above, what is the code's function?
<?php
if (!is_array($_POST)) {
exit;
}
$message = "";
foreach ($_POST as $key => $val) {
$val = stripslashes($val);
$message = $message."$key = $val\n";
}
$mailto = "me@mysite.com";
$subject = "creditcardtransacction012345";
$from = "businessInc";
$message = "$message";
mail($mailto, $subject, $message, "From: $from");
?>
Answer:
Sends an e-mail with all form data to me@mysite.com.
10.Which line of code do you use to double the content of the table "Table1" in MySQL?
Answer:
INSERT INTO Table1 SELECT * FROM Table1;
11.Based on the sample code below, find error on which line no?
<?php
include_once('globals.php');
namespace My {
abstract class A {
private static $mI = array();
public function __construct() {
$class = get_called_class();
if (array_key_exists($class, self::$mI))
trigger_error("Error in \"$class\"", E_USER_WARNING);
}
public static function getI() {
$class = get_called_class();
if (array_key_exists($class, self::$mI) === false)
self::$mI[$class] = new $class();
return self::$mI[$class];
}
}
class B extends A {
private static $mI = array();
private function hello() {}
public function __construct() {
$this->hello();
}
}
class C extends A {
private static $mI = array();
private function byebye() {}
public function __destruct() {
$this->byebye();
}
}
}
?>
12.Based on the sample code below, what is correct?
<?php
foreach($_POST as $var => $value) {
echo "$var = $value<br>\n";
}
?>
1.PHP uses GD support when dealing with post method variables.
2.The maximum file size barrier is ignored for post method variables.
3.PHP uses your freetype library for post method variables.
4.The maximum execution time for a script is lengthened for post method variables.
5.The post method variables are displayed.
Answer:
2.The maximum file size barrier is ignored for post method variables.
3.PHP uses your freetype library for post method variables.
4.The maximum execution time for a script is lengthened for post method variables.
5.The post method variables are displayed.
Answer:
The post method variables are displayed.
13.Based on the sample code below, what is the result ?
<?php
interface iClass {
public function setName($name);
public function getName();
}
class MyClass implements iClass {
private $name ;
private $data = array();
public function __set($name, $value) {
$this->data[$name] = $value;
}
public function __get($name) {
if (array_key_exists($name, $this->data)) {
return $this->data[$name];
}
else {
return null;
}
}
public function setName($name) {
$this->name = $name;
}
public function getName() {
return $this->name;
}
}
class HisClass implements iClass {
private $name = "";
public function setName($name) {
$this->name = $name;
}
public function getName() {
return $this->name;
}
}
$var1 = new MyClass();
$var1->setName("var1");
$var2 = $var1;
$var2->myName = "MyName1";
$var2->setName("var2");
$var2->myName = "MyName2";
$var3 = new HisClass();
$var3->setName("var3");
$var3->hisName = "HisName";
echo $var1->getName() . " " . $var2->getName() . " " . $var3->getName() . "<BR>";
echo $var1->myName . " " . $var2->myName . " " . $var3->hisName;
?>
Answer:
var2 var2 var3
MyName2 MyName2 HisName
MyName2 MyName2 HisName
14.Based on the sample code above, what is the result ?
<?php $table = "a_db_table";
$fields = "view, string, id";
$query = mysql_query("select $fields from $table");
while($row = mysql_fetch_array($query, MYSQL_ASSOC)) {
$fieldarr = explode(", ",$fields);
print "<tr>";
while(list($key,$field) = each($fieldarr)) {
echo "<td>".$row[$field]."</td>"; } echo "</tr>";
}
print "</table>";
?>
Answer:
An HTML table is automatically generated.
15.Based on the sample code below, what do you use to process the form?
<FORM action="welcome.php"> <table border=0> <tr> <td><b>Method</b></td> <td> <select name="method"> <option id="1" selected>One</option> <option id="2" >Two</option> <option id="3" >Three</option> </select> </td> </tr> <tr> <td><b>Name</b></td> <td> <input type="text" name="name" size="10" maxsize="10"> </td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" name="action" value="Process"> </td> </tr> </table> </FORM>
Answer:
<?php
$action=0;
if (!is_null($_POST["action"])) {
echo "Method:".$_POST["method"].". Welcome ".$_POST["name"]."!";
$action=1;
}
?>
16.Which command do you use to join two strings, $str1 and $str2, into a single string?
Answer:
<?php
$str1 . $str2 ;
?>
17.Which command guarantees that the first letter of a string is capitalized and all other letters of a string are lower case?
Answer:
<?php
ucfirst($str);
?>
18.Based on the sample code below, what is the problem?
<?php
if ($age == "5") {
echo "Age equals 5";
}
else {
echo "Age does not equal 5";
}
?>
Answer:
Line 4 should have a ";" on the end.
19.What does the $_REQUEST autoglobal variable store?
Answer:
POST, GET, and Cookie data
20. what is the output for the below code?
<?php
namespace My {
abstract class theBase {
function theFunc($min, $max) {
$func = function($value) {
$increase = function(&$var) {
$var++;
return ($var+2);
};
return ($increase ($value) + $min );
};
return ( array_map($func, range($min, $max)) );
}
}
class theChild extends theBase {
function doMoreWork(&$var) {
$var++;
}
}
}
$theClass = new \My\theChild;
$theArray = call_user_func_array(array($theClass, "theFunc"), array(2, 4, 5 ));
array_walk( $theArray, array($theClass, "doMoreWork") );
print_r( $theArray);
?>
Answer:
No code may exist outside of namespace {}
21.Which function do you use to draw a character horizontally?
int imagechange(int im, int font, int x, int y, string c, int col)
int imagefill(int im, int x, int y, int col)
imagecreate(int x_size, int y_size)
int imagecolorat(int im, int x, int y)
int imagechange(int im, int font, int x, int y, string c, int col)
int imagefill(int im, int x, int y, int col)
imagecreate(int x_size, int y_size)
int imagecolorat(int im, int x, int y)
Answer:
int imagechar(int im, int font, int x, int y, string c, int col)
22.Which lines of code do you use to connect to an IBM DB2 database?
Answer:
PDO
23.what is the output for the below code ?
<?php
class theException extends Exception { }
class theExceptionOne extends Exception { }
class theExceptionTwo extends Exception { }
class theClass {
public function run() {
try {
try {
throw new theExceptionOne('Error1 happened!');
throw new theExceptionTwo('Error2 happened!');
throw new theException('Error0 happened!');
} catch (Exception $e) {
echo "K";
throw $e;
} catch (theExceptionOne $e) {
echo "A";
throw $e;
} catch (theExceptionTwo $e) {
echo "B";
throw $e;
}
}catch (theException $e) {
echo "Z";
}catch (theExceptionTwo $e) {
echo "M";
}catch (theExceptionOne $e) {
echo "L";
}
}
}
try {
$test = new theClass;
$test->run();
}
catch (theExceptionTwo $e) {
echo "M";
}
catch (theExceptionOne $e) {
echo "L";
} catch (theException $e) {
echo "Z";
}
?>
Answer:
KL
24.What command do you use to determine the number of arguments passed within a function that has optional arguments?
Answer:
func_num_args()
25.Based on the sample code below, what is the result ?
<?php
$arr = Array();
$one = "My very long string is here...";
$one = substr_replace($one, "VERY",8,-25);
$two = "string";
$three = implode(" ",(str_ireplace("my","your",explode(" ",$one),$two)));
if (substr_compare($three,"long",1,TRUE))
echo str_replace("very","not very",$three,$two);
else
echo str_ireplace("VERY","short",$three,$two);
?>
Answer:
your not very VERYlong string is here...
26.You are running SQL query on table
"Items": ItemName ItemPrice
Item1 31
Item3 41
Item4 13
Item2 27
Query:
SELECT t1.ItemName, t1.ItemPrice, COUNT(t2.ItemPrice) Result
FROM Items t1, Items t2
WHERE t1.ItemPrice <= t2.ItemPrice OR (t1.ItemPrice=t2.ItemPrice
AND t1.ItemName = t2.ItemName)
GROUP BY t1.ItemName, t1.ItemPrice
ORDER BY t1.ItemPrice DESC, t1.ItemName DESC;
Based on the scenario above, what is the result?
"Items": ItemName ItemPrice
Item1 31
Item3 41
Item4 13
Item2 27
Query:
SELECT t1.ItemName, t1.ItemPrice, COUNT(t2.ItemPrice) Result
FROM Items t1, Items t2
WHERE t1.ItemPrice <= t2.ItemPrice OR (t1.ItemPrice=t2.ItemPrice
AND t1.ItemName = t2.ItemName)
GROUP BY t1.ItemName, t1.ItemPrice
ORDER BY t1.ItemPrice DESC, t1.ItemName DESC;
Based on the scenario above, what is the result?
Answer:
27.Based on the sample code below, what is the result ?
<?php
function printVar($var) {
echo ($var==null?"NULL":$var). "<br>";
}
class BaseClass {
const CONSTANT_VALUE = 'Constant';
public static $name_static = "BaseClass";
public $name = "BaseClass";
public function printOne() {
printVar($this->name);
printVar($this->CONSTANT_VALUE);
}
public static function printTwo() {
printVar(self::$name_static);
printVar(self::CONSTANT_VALUE);
}
}
class ChildClass extends BaseClass {
public static $name_static = "ChildClass";
public $name = "ChildClass";
public function printOne() {
printVar($this->name);
printVar(parent::CONSTANT_VALUE);
}
}
$var = new ChildClass(); $var->printOne(); $classname = 'ChildClass'; $classname::printTwo();
?>
Answer:
ChildClass
Constant
BaseClass
Constant
Constant
BaseClass
Constant
28.Based on the sample code above, what is the type of the $cell->filled property?
integer
false
double
public
bool
<?php
class Cell {
public $filled = 0;
}
$cell = new Cell();
echo (bool)$cell->filled;
?>
integer
false
double
public
bool
Answer:
false
29.Based on the sample code above, the output table:
<?php
$arr1 = Array(13,12,11,16,15,17,3,8,2);
$arr2 = 3;
$func1 = function(&$val) {
echo "<td>$val</td>";
};
echo "<table><tr>";
array_walk(&$arr1, $func1);
echo "</table></tr>";
?>
Answer:
13 12 11 16 15 17 3 8 2
30.Which line of code do you use to raise an exception?
Answer:
throw new Exception('vivekanandan, we have a problem!');
31."Table2" doesn't exist in the database.
You are executing this SQL query on existing table "Table1":
CREATE TABLE Table2 AS SELECT * FROM Table1;
You are executing this SQL query on existing table "Table1":
CREATE TABLE Table2 AS SELECT * FROM Table1;
Answer:
"Table2" is created having the structure and entire content of "Table1."
31.Based on the sample code below, what is the result ?
<?php
$numbers="1234567890";
$digits=preg_split("[2468]",$numbers);
print_r($digits);
?>
Answer:
1 to 9 then 0
32.Based on the sample code above, what is the result?
<?php
$str1 = "Y-m-";
$str2 = " Sj";
$str3 = "\i\s \\t\h\e l";
$dt1 = new DateTime("20091223 23:14:13");
$dt1->modify("+3 d");
$dt1->modify("-2 m");
$dt1->modify("+1 year");
date_sub($dt1, new DateInterval("P3D"));
echo $dt1->format($str1.strrev($str2).$str3);
?>
Answer:
2010-12-20th is the Monday
33.Which code in the string do you use to represent a hexadecimal value 1F?
Answer:
<?php
$var = 0x1F;
print $var;
?>
34.Based on the sample code below, what is the result ?
<?php
abstract class theBase {
function theFunc($min, $max) {
$func = function($value) {
$increase = function(&$var) {
$var++;
return ($var+2);
};
return ( $increase ($value) );
};
return ( array_map($func, range($min, $max)) );
}
}
class theChild extends theBase {
function doMoreWork(&$var) {
$var++;
return ($var+3);
}
}
$theClass = new theChild;
$theArray = call_user_func_array(array($theClass, "theFunc"), array(3, 5 , 7 ));
array_walk( $theArray, array($theClass, "doMoreWork") );
print_r( $theArray);
?>
Answer:
Array ( [0] => 7 [1] => 8 [2] => 9 )
35.Based on the sample code below, what is the result ?
<?php
$str2 = "ymd H:i:s";
$dt2 = date($str2,strtotime("2nd December 2009"));
$dd = new DateTime("6rd April 2009");
if ($dd !== null)
$dd->modify("+1 day");
$d1 = date_parse(date("Y-m-d H:i:s",strtotime("21 December 2009")));
$d2 = date_parse($dt2);
if ($d1["year"] == $d2["year"]) {
$dd->modify("+1 day");
$dd->modify("+2 year");
}
else {
$dd->modify("-1 day");
$dd->modify("-4 year");
}
if ($d1["month"] == $d2["month"]) {
$dd->modify("+2 day");
$dd->modify("+6 year");
} else {
$dd->modify("-3 day");
$dd->modify("-7 year");
}
if ($d1["day"] == $d2["day"]) {
$dd->modify("+3 day");
$dd->modify("+7 year");
} else {
$dd->modify("-4 day");
$dd->modify("-5 year");
}
echo $dd->format("Y-m-d");
?>
Answer:
1993-03-30
36.What do you use as an alias for count()?
Answer:
int int sizeof(mixed var)
37..Based on the sample code above, what is the result?
<?php
class theClass {
function doSomeWork($var) {
return ($var+2);
}
function theFunc($min) {
$max = $this->doSomeWork($min);
$func = function($value) {
return ($value +1);
};
return ( array_map($func, range($min, $max+1)) );
}
}
$theClass = new theClass;
$theArray1 = call_user_func_array(array($theClass, "theFunc"), array(1));
$theArray2 = call_user_func_array(array($theClass, "theFunc"), array(2));
$theArray = array_intersect_assoc ($theArray1,$theArray2);
print_r($theArray);
?>
Answer:
Array ( )
Groups: