Tricky PHP Questions & Answers on “Arrays – 2”.
1. What will be the output of the following PHP code?
-
-
$cars = array("Volvo", "BMW", "Toyota");
-
echo "I like " . $cars[2] . ", " . $cars[1] . " and " . $cars[0] . ".";
-
?>
a) I like Volvo, Toyota and BMW
b) I like Volvo, BMW and Toyota
c) I like BMW, Volvo and Toyota
d) I like Toyota, BMW and Volvo
Answer: d
Clarification: The order of elements defined. In the echo statement when we call the elements of array using its index, it will be printed accordingly. As index ‘0’ indicates ‘Volvo’, ‘1’ for ‘BMW’ and ‘2’ for Toyota’.
2. What will be the output of the following PHP code?
-
-
$fname = array("Peter", "Ben", "Joe");
-
$age = array("35", "37", "43");
-
$c = array_combine($age, $fname);
-
print_r($c);
-
?>
a) Array (Peter Ben Joe)
b) Array ([Peter] => 35 [Ben] => 37 [Joe] => 43)
c) Array (35 37 43)
d) Array ([35] => Peter [37] => Ben [43] => Joe)
Answer: d
Clarification: Here “keys” array is $age and “values” array is $fname. The function array_combine() will create an array by using the elements from one “keys” array and one “values” array. So when variable c is called, it will print keys and values.
3. What will be the output of the following PHP code?
-
-
$a=array("A","Cat","Dog","A","Dog");
-
$b=array("A","A","Cat","A","Tiger");
-
$c=array_combine($a,$b);
-
print_r(array_count_values($c));
-
?>
a) Array ( [A] => 5 [Cat] => 2 [Dog] => 2 [Tiger] => 1 )
b) Array ( [A] => 2 [Cat] => 2 [Dog] => 1 [Tiger] => 1 )
c) Array ( [A] => 6 [Cat] => 1 [Dog] => 2 [Tiger] => 1 )
d) Array ( [A] => 2 [Tiger] => 1 )
Answer: d
Clarification: The function The array_count_values() counts all the values of an array and the The function array_combine() will create an array by using the elements from one “keys” array and one “values” array.
4. 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", "h" => "orange");
-
$a3 = array("i" => "orange");
-
$a4 = array_merge($a2, $a3);
-
$result = array_diff($a1, $a4);
-
print_r($result);
-
?>
a) Array ( [d] => yellow )
b) Array ( [i] => orange )
c) Array ( [h] => orange )
d) Array ( [d] => yellow [h] => orange )
Answer: a
Clarification: The array_diff() function compares the values of two (or more) arrays, and returns the differences. This function compares the values of two (or more) arrays, and return an array that contains the entries from array1 that are not present in other arrays (array2, array3, etc).
5. What will be the output of the following PHP code?
-
-
$a1 = array("red", "green");
-
$a2 = array("blue", "yellow");
-
$a3 = array_merge($a1, $a2);
-
$a4 = array("a", "b", "c", "d");
-
$a = array_combine($a4, $a3);
-
print_r($a);
-
?>
a) Array ( [a] => blue [b] => yellow [c] => red [d] => green )
b) Array ( [0] => blue [1] => yellow [2] => red [3] => green )
c) Array ( [0] => red [1] => green [2] => blue [3] => yellow )
d) Array ( [a] => red [b] => green [c] => blue [d] => yellow )
