This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “Arrays – 2”.
1. What will be the output of the following PHP code?
<?php
$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
View Answer
Explanation: 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?
<?php
$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)
View Answer
Explanation: 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?
<?php
$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 )
View Answer
Explanation: 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?
<?php
$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 )
View Answer
Explanation: 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?
<?php
$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 )
View Answer
Explanation: The function array_merge() merges one or more arrays into one array. If in the function array_merge(), two or more array elements have the same key, the last one overrides the others. The function array_combine() will create an array by using the elements from one “keys” array and one “values” array. The program is the basic combined application of array_combine() and array_merge().
6. What will be the output of the following PHP code?
<?php
$a = array("a" => "india", "b" => "brazil", "c" => "china");
echo array_shift($a);
echo "<br>";
array_pop($a);
print_r($a);
?>
a)
india Array ( [b] => Brazil )
b)
india Array ( [a] => brazil )
c)
china Array ( [a] => india )
d)
china Array ( [a] => brazil )View Answer
Explanation: The function array_shift() removes the first element from an array, and it returns the value of the removed element and the function array_pop() deletes the last element of an array. So “a” => “India”, “c” => “China” will be deleted and “b” => “Brazil” will be printed.
7. What will be the output of the following PHP code?
<?php
$a1 = array_fill(1, 4, "hello");
$b1 = array_fill(5, 1, "php");
$a2 = array_merge($a1, $a2);
print_r($a2);
echo "<br>";
print_r($b1);
?>
a)
Array ( [1] => hello [4] => hello [5] => php ) Array ( [5] => php )
b)
Array ( [1] => hello [2] => hello [3] => hello [4] => hello ) Array ( [5] => php )
c)
Array ( [1] => hello [2] => hello [3] => hello [4] => hello [5] => php ) Array ( [5] => php )
d)
Array ( [1] => hello [2] => hello [3] => hello [4] => hello ) Array ( [1] => php )View Answer
Explanation: Usage of array_fill() and array_merge() functions.
8. What will be the output of the following PHP code?
<?php
$names = array("Sam", "Bob", "Jack");
echo $names[0] . "is the brother of " . $names[1] . " and " . $names[1] . ".";
?>
a) Sam is the brother of Bob and Jack.
b) Sam is the brother of Bob and Bob.
c) Sam is the brother of Jack and Bob.
d) Error
View Answer
Explanation: Simple definition of array and using it in a string. We have used $names[1] twice and hence Bob appears twice.
9. What will be the output of the following PHP code?
<?php
$names = array("Sam", "Bob", "Jack");
echo $names[0]." is the brother of ".$names[1]." and ".$names[1].".".$brother;
?>
a) Sam is the brother of Bob and Bob.$brother
b) Sam is the brother of Bob and Bob.
c) $brother
d) Error
View Answer
Explanation: $brother undeclared.
10. What will be the output of the following PHP code?
<?php
$place = array("NYC", "LA", "Paris");
array_pop($place);
$place1 = array("Paris");
$place = array_merge($place, $place1);
print_r($place);
?>
a) Array ( [0] => LA [1] => Paris [2] => Paris )
b) Array ( [0] => NYC [1] => LA [2] => Paris)
c) Array ( [0] => NYC [1] => LA [2] => Paris [3] => Paris )
d) Array ( [0] => LA [1] => Paris )
View Answer
Explanation: array_merge() and array_pop() yields that result.
Sanfoundry Global Education & Learning Series – PHP Programming.
To practice all questions on PHP Programming, here is complete set of 1000+ Multiple Choice Questions and Answers on PHP.
- Check PHP Books
- Practice MCA MCQs
- Check Information Technology Books
- Check MCA Books
- Practice Programming MCQs