Tough PHP Questions & Answers on “Arrays – 3”.
1. What will be the output of the following PHP code?
-
-
$age = array("Harry" => "21", "Ron" => "23","Malfoy" => "21");
-
array_pop($age);
-
print_r(array_change_key_case($age, CASE_UPPER));
-
?>
a) Array ( [Harry] => 21 [Ron] => 23 [Malfoy] => 21 )
b) Array ( [HARRY] => 21 [RON] => 23 [MALFOY] => 21 )
c) Array ( [HARRY] => 21 [RON] => 23 )
d) Array ( [Harry] => 21 [Ron] => 23 )
Answer: c
Clarification: The function array_pop() will delete the last element of an array. So Malfoy => 21 will be deleted and the function array_change_key_case() will change all keys in an array to lowercase or uppercase.
2. What will be the output of the following PHP code?
-
-
$a1 = array("a" => "red", "b" => "green", "c" => "blue", "d" => "yellow");
-
$result = array_flip($a1);
-
print_r($result);
-
?>
a) Array ( [red] => red [green] => green [blue] => blue [yellow] => yellow )
b) Array ( [a] => a [b] => b [c] => c [d] => d )
c) Array ( [red] => a [green] => b [blue] => c [yellow] => d )
d) Array ( [a] => red [b] => green [c] => blue [d] => yellow )
Answer: c
Clarification: The function array_flip() flips/exchanges all keys with their associated values in an array. So, in the above program “a” will be flipped with “red”, “b” will be flipped with “green” and so on.
3. What will be the output of the following PHP code?
-
-
$a1 = array("a" => "red", "b" => "green", "c" => "blue", "d" => "yellow");
-
$a2 = array("e" => "red","f" => "green", "g" => "blue");
-
$result = array_intersect($a1, $a2);
-
print_r($result);
-
?>
a) Array ( [a] => red [b] => green [c] => blue )
b) Array ( [a] => red [b] => green [c] => blue [d] => yellow )
c) Array ( [e] => red [f] => green [g] => blue )
d) Array ( [a] => red [b] => green [c] => blue [d] => yellow [e] => red [f] => green [g] => blue )
Answer: a
Clarification: The function array_intersect() compares the values of two (or more) arrays, and returns the matches. So, in the above program values of a1 and a2 will be compared and the values present in both the arrays will be the returned.
4. What will be the output of the following PHP code?
-
-
$a = array(12, 5, 2);
-
echo(array_product($a));
-
?>
a) 024
b) 120
c) 010
d) 060
Answer: b
Clarification: The array_product() function calculates and returns the product of an array.
5. What will be the output of the following PHP code?
-
-
$a = array("a" => "Jaguar", "b" => "Land Rover",
-
"c" => "Audi", "d" => "Maseratti");
-
echo array_search("Audi", $a);
-
?>
a) a
b) b
c) c
d) d
Answer: c
Clarification: The array_search() function searches for the element and returns the key of that element.
6. What will be the output of the following PHP code?
-
-
$city_west = array("NYC", "London");
-
$city_east = array("Mumbai", "Beijing");
-
print_r(array_replace($city_west, $city_east));
-
?>
a) Array ( [1] => Mumbai [0] => Beijing )
b) Array ( [0] => NYC [1] => London )
c) Array ( [1] => NYC [0] => London )
d) Array ( [0] => Mumbai [1] => Beijing )
Answer: d
Clarification: The function array_replace() replaces the values of the first array with the values from following arrays. So, in the above program the values of city_west will be replaced with city_east.
7. What will be the output of the following PHP code?
-
-
$people = array("Peter", "Susan", "Edmund", "Lucy");
-
echo pos($people);
-
?>
a) Lucy
b) Peter
c) Susan
d) Edmund
