PHP Multiple Choice Questions on “Functions – 2”.
1. What will be the output of the following PHP code?
-
-
function sum($num1, $num2)
-
{ -
$total = $num1 + $num2;
-
echo "chr($total)";
-
} -
$var1 = "sum";
-
$var1(5, 44);
-
?>
a) Error
b) 49
c) 1
d) Sum
Answer: c
Clarification: It is possible to call a function using a variable which stores the function name also the chr() function returns a character from the specified ASCII value.
2. What will be the output of the following PHP code?
-
-
function sum($num1, $num2)
-
{ -
$total = $num1 + $num2;
-
echo "cos($total)";
-
} -
sum(5,-5);
-
?>
a) 0
b) 1
c) 0.5
d) -0.5
Answer: b
Clarification: cos() gives the cos value of the argument. Here the function returns 1.
3. What will be the output of the following PHP code?
-
-
function b()
-
{ -
echo "b is executed";
-
} -
function a()
-
{ -
b();
-
echo "a is executed";
-
b();
-
} -
a();
-
?>
a) b is executedb is executedb is executed
b) b is executeda is executed
c) a is executed
d) b is executeda is executedb is executed
Answer: d
Clarification: Simple order of execution.
4. What will be the output of the following PHP code?
-
-
function sum($x, $y)
-
{ -
$z = $x + $y;
-
return $z;
-
} -
echo "5 + 10 = " . sum(7,13) . "
"; -
echo "7 + 13 = " . sum(2,4) . "
"; -
echo "2 + 4 = " . sum(5,10);
-
?>
a)
5 + 10 = 15 2 + 4 = 6 7 + 13 = 20
b)
7 + 13 = 20 5 + 10 = 15 2 + 4 = 6
c)
5 + 10 = 15 7 + 13 = 20 2 + 4 = 6
d)
5 + 10 = 20 7 + 13 = 6 2 + 4 = 15
Answer: d
Clarification: The function calls are jumbled.
5. What will be the output of the following PHP code?
-
-
function addFive($num)
-
{ -
$num += 5;
-
} -
function addSix(&$num)
-
{ -
$num += 6;
-
} -
$orignum = 10;
-
addFive( &$orignum );
-
echo "Original Value is $orignum
"; -
addSix( $orignum );
-
echo "Original Value is $orignum
"; -
?>
a)
Original Value is 15 Original Value is 21
b)
Original Value is 15 Original Value is 21
c)
Original Value is 15 Original Value is 15
d) None Of The mentioned
Answer: b
Clarification: addSix() passes value of the variable by reference.
6. 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: Functions returns value 30.
7. What will be the output of the following PHP code?
-
-
function sayHello()
-
{ -
echo "HelloWorld
"; -
} -
$function_holder = "sayHello";
-
$function_holder();
-
?>
a) No Output
b) Error
c) sayHello
d) HelloWorld
Answer: d
Clarification: It is possible to assign function names as strings to variables and then treat these variables exactly as you would the function name itself.
8. What will be the output of the following PHP code?
-
-
function one()
-
{ -
echo " this works";
-
function two()
-
{ -
echo "this too works";
-
} -
} -
one();
-
two();
-
?>
a) error
b) this works
c) this worksthis too works
d) this works this too works
Answer: c
Clarification: Two is declared in one and is called after one. Hence it works.
9. What will be the output of the following PHP code?
-
-
function do($myString)
-
{ -
echo strpos($myString, "donkey",0);
-
} -
do("The donkey looks like a horse.");
-
?>
a) 4
b) 5
c) 2
d) None of the mentioned
Answer: a
Clarification: Donkey starts from position 4 in string.
10. What will be the output of the following PHP code?
-
-
function one()
-
{ -
define("const","I am awesome!");
-
echo constant("const");
-
} -
one();
-
?>
a) I am awesome!!
b) const
c) const, I am awesome!!
d) “const”,”I am awesome!”
