PHP Question Paper on “Functions – 6”.
1. What will be the output of the following PHP code?
-
-
function colour()
-
{ -
$colors = array("red", "green", "blue", "yellow");
-
foreach ($colors as $value)
-
{ -
echo "$value
"; -
} -
} -
colour();
-
?>
a)
red green blue yellow
b)
green blue yellow red
c)
red blue yellow green
d)
red green yellow blue
Answer: a
Clarification: For each function causes the program to loop through each array value once.
2. What will be the output of the following PHP code?
-
-
function addFunction($num1, $num2)
-
{ -
$sum = $num1 + $num2;
-
return $sum;
-
} -
$return_value = addFunction(10, 20);
-
echo "Returned value from the function : " .$return_value
-
?>
a) Returned value from the function : $return_value
b) Error
c) Returned value from the function : 30
d) Returned value from the function :
Answer: c
Clarification: Function returns 30.
3. What will be the output of the following PHP code?
-
-
function time($string)
-
{ -
echo strtr("Towe Pa55", "ow5", $string);
-
} -
time("ims");
-
?>
a) Time Pa55
b) Towe Pa55
c) Towe Pass
d) Time Pass
Answer: d
Clarification: The strtr() function translates certain characters in a string.
4. What will be the output of the following PHP code?
-
-
function constant()
-
{ -
define("GREETING", "Welcome to Narnia");
-
echo greeting;
-
} -
?>
a) Welcome to Narnia
b) greeting
c) GREETING
d) ERROR
Answer: d
Clarification: By default constants are case sensitive. Hence an error will arise.
5. What will be the output of the following PHP code?
-
-
function constant()
-
{ -
define("GREETING", "Welcome to Narnia",true);
-
echo greeting;
-
} -
?>
a) Welcome to Narnia
b) greeting
c) GREETING
d) ERROR
Answer: a
Clarification: By default constants are case sensitive. But the third parameter in define(), if set to true, makes constants case insensitive.
6. What will be the output of the following PHP code?
-
-
function start($string)
-
{ -
if ($string < 45)
-
return 20;
-
else -
return 40;
-
} -
$t = start(90);
-
if ($t < 20)
-
{ -
echo "Have a good day!";
-
} -
else -
{ -
echo "Have a good night!";
-
} -
?>
a) Have a good day!
b) Have a good night!
c) ERROR
d) None of the mentioned
Answer: b
Clarification: Function returns 40. This is greater than 20, hence the output.
7. What will be the output of the following PHP code?
-
-
function case()
-
{ -
ECHO "Hello World!
"; -
echo "Hello World!
"; -
EcHo "Hello World!
"; -
} -
case();
-
?>
a) Hello World!
b)
Hello World! Hello World!
c)
Hello World! Hello World! Hello World!
d) None of the mentioned
Answer: c
Clarification: Functions, keywords etc in php are case insensitive.
8. What will be the output of the following PHP code?
-
-
function email()
-
{ -
$email = ’user@yahoo.com’;
-
$new = strstr($email, ‘@');
-
echo $new; -
} -
email(); -
?>
a) user
b) [email protected]
c) @yahoo.com
d) yahoo.com
Answer: c
Clarification: The strstr() function searches for the first occurrence of a string inside another string.
9. What will be the output of the following PHP code?
-
-
function string()
-
{ -
echo strstr("Hello world!", 111);
-
} -
string();
-
?>
a) o world!
b) Hello world!
c) 111
d) No Output
Answer: a
Clarification: 111 is the ASCII value of o.
10. What will be the output of the following PHP code?
-
-
function CalAll($x,$y)
-
{ -
echo ($x + $y);
-
echo "
"; -
echo ($x - $y);
-
echo "
"; -
echo ($x * $y);
-
echo "
"; -
echo ($x / $y);
-
echo "
"; -
echo ($x % $y);
-
} -
$x = 10;
-
$y = 6;
-
CalcAll();
-
?>
a)
4 60 1.6666666666667 4 16
b)
16 4 60 1.6666666666667 4
c)
4 16 4 60 1.6666666666667
d)
1.6666666666667 4 16 4 60
