lOMoAR cPSD| 49343224
Downloaded by Vincent master ()
, lOMoAR cPSD| 49343224
THIS DOCUMENT CONTAINS 200+ ANSWERED QUESTIONS
Question 1 (2 marks)
What is the output of the following code
when executed? function func_1(){
return "func_1 is executed";
}
function
func_2()
{
func_1()
;
echo "func_2 is executed";
}
f
u
n
c
_
2
(
)
;
1. func_1 is executedfunc_2 is executed
2. func_1 is executed
3. func_2 is executed
4. func_2 is executedfunc_1 is executed
Answer: (3)
Refer to chapter 13.
Both functions get executed, however the string returned by func_1()
never gets displayed.
Downloaded by Vincent master ()
, lOMoAR cPSD| 49343224
Question 2 (2 marks)
What is the output displayed by the
following code? function
calculate($arr, &$num){
$result
= 0;
foreach($arr
as $value){
$result += $value;
}
$num = $result / count($arr);
}
$arr = array(10,20,30,40,50);
$num =
0;
calcul
ate($a
rr,$nu
m);
echo
$num;
1. 150
2. 30
3. 15
4. 0
Answer: (2)
Refer to pages 392 to 393 (section ‘How to pass arguments by value
and by reference’) in chapter 13. In this code $num is passed by
reference and in the function, it gets initialised to 30
((10 + 20 + 30 + 40 + 50)/5), which then displayed outside of the
function.
Question 3 (2 marks)
What is the output
displayed by the following
code? function
calculate(){ $num
= func_num_args();
$result = 0;
Downloaded by Vincent master ()
, lOMoAR cPSD| 49343224
for($i = 0; $i <
$num; $i++){ $result
+= func_get_arg($i);
}
return $result * $num;
}
echo calculate(3,2,1,0);
1. 0
2. 7
3. 21
4. 24
Answer: (4)
Refer to pages 400 to 401 (section ‘How to use variable-length
parameter lists’) in chapter 13. The function makes use of variable-
length parameter lists. It adds all the arguments and multiplies the sum
by the number of passed arguments. So 3+2+1+0 is 6 and 6*4 is 24.
Question 4 (1 mark)
Within a function, you can use the ________________ keyword if you
need to refer to a variable that’s declared outside the function.
1. global
2. return
3. arguments
4. void
Answer: (1)
Refer to pages 394 to 395 (section ‘How variable scope works’)
in chapter 13. Question 5 (2 marks)
Which of the following options is correct about functions in PHP?
1. One can define a function without a name. Similarly, one can
define a function without a return statement.
2. When an argument is passed by value, the original variable is
sent to the function. Then any changes to the parameter in the function
causes changes in the value stored in the original variable.
3. If a function is defined without any parameters, it means that no
arguments can be passed to the function when a function call is made.
4. A dedicated PHP file can be created to define multiple functions
but a namespace must be created for the functions in the file.
Answer: (1)
Downloaded by Vincent master ()
Downloaded by Vincent master ()
, lOMoAR cPSD| 49343224
THIS DOCUMENT CONTAINS 200+ ANSWERED QUESTIONS
Question 1 (2 marks)
What is the output of the following code
when executed? function func_1(){
return "func_1 is executed";
}
function
func_2()
{
func_1()
;
echo "func_2 is executed";
}
f
u
n
c
_
2
(
)
;
1. func_1 is executedfunc_2 is executed
2. func_1 is executed
3. func_2 is executed
4. func_2 is executedfunc_1 is executed
Answer: (3)
Refer to chapter 13.
Both functions get executed, however the string returned by func_1()
never gets displayed.
Downloaded by Vincent master ()
, lOMoAR cPSD| 49343224
Question 2 (2 marks)
What is the output displayed by the
following code? function
calculate($arr, &$num){
$result
= 0;
foreach($arr
as $value){
$result += $value;
}
$num = $result / count($arr);
}
$arr = array(10,20,30,40,50);
$num =
0;
calcul
ate($a
rr,$nu
m);
echo
$num;
1. 150
2. 30
3. 15
4. 0
Answer: (2)
Refer to pages 392 to 393 (section ‘How to pass arguments by value
and by reference’) in chapter 13. In this code $num is passed by
reference and in the function, it gets initialised to 30
((10 + 20 + 30 + 40 + 50)/5), which then displayed outside of the
function.
Question 3 (2 marks)
What is the output
displayed by the following
code? function
calculate(){ $num
= func_num_args();
$result = 0;
Downloaded by Vincent master ()
, lOMoAR cPSD| 49343224
for($i = 0; $i <
$num; $i++){ $result
+= func_get_arg($i);
}
return $result * $num;
}
echo calculate(3,2,1,0);
1. 0
2. 7
3. 21
4. 24
Answer: (4)
Refer to pages 400 to 401 (section ‘How to use variable-length
parameter lists’) in chapter 13. The function makes use of variable-
length parameter lists. It adds all the arguments and multiplies the sum
by the number of passed arguments. So 3+2+1+0 is 6 and 6*4 is 24.
Question 4 (1 mark)
Within a function, you can use the ________________ keyword if you
need to refer to a variable that’s declared outside the function.
1. global
2. return
3. arguments
4. void
Answer: (1)
Refer to pages 394 to 395 (section ‘How variable scope works’)
in chapter 13. Question 5 (2 marks)
Which of the following options is correct about functions in PHP?
1. One can define a function without a name. Similarly, one can
define a function without a return statement.
2. When an argument is passed by value, the original variable is
sent to the function. Then any changes to the parameter in the function
causes changes in the value stored in the original variable.
3. If a function is defined without any parameters, it means that no
arguments can be passed to the function when a function call is made.
4. A dedicated PHP file can be created to define multiple functions
but a namespace must be created for the functions in the file.
Answer: (1)
Downloaded by Vincent master ()