Q: 1 What will be the output of the following PHP code?
<?php
$x = 5;
$y = 10;
function fun()
{
$y = $GLOBALS[‘x’] + $GLOBALS[‘y’];
}
Fun();
echo $y;
?>
5
10
15
25
[ Option B ]
In this PHP code, the variables $x = 5 and $y = 10 are defined globally. Inside the function fun(), a new local variable $y is created and assigned the value of $GLOBALS['x'] + $GLOBALS['y'], which is 15.
However, since no global $y; keyword is used, this local $y does not affect the global $y. After the function call, the global $y still holds the value 10, so the output is 10.
Q: 2 What will be the output of the following PHP program?
<?php
$fruits = array(“apple”, “orange”, array (“pear”, “mango”), “banana”);
print (count($fruits,1));
?>
6
5
4
3
[ Option A ]
In this PHP program, the array $fruits contains four elements: "apple", "orange", a nested array ["pear", "mango"], and "banana".
By default, the count() function in PHP counts only the top-level elements, which would give 4. However, since the program uses count($fruits, 1), the second parameter enables recursive counting, meaning elements inside nested arrays are also included in the count.
The nested array ["pear", "mango"] has 2 elements, so the total becomes 4 + 2 = 6. Therefore, the output of the program is 6.
Thank you so much for taking the time to read my Computer Science MCQs section carefully. Your support and interest mean a lot, and I truly appreciate you being part of this journey. Stay connected for more insights and updates! If you'd like to explore more tutorials and insights, check out my YouTube channel.
Don’t forget to subscribe and stay connected for future updates.