PHP Questions and Answers Part-9

1. What will be the output of the following PHP code?
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>
a) I like Volvo BMW and Toyota.
b) I like Volvo, BMW and Toyota)
c) I like Volvo, BMW and Toyota.
d) I like. Volvo.,. BMW. and. Toyota)

Answer: b
Explanation: The array() function is used to create an array. Each elements are assigned ab index as the rule of an array. So, calling $cars[0] will print element at index 0 and so on.

2. What will be the output of the following PHP code?
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
print_r(array_change_key_case($age, CASE_UPPER));
?>
a) Array ( [Peter] => 35 [Ben] => 37 [Joe] => 43 )
b) Array ( [peter] => 35 [ben] => 37 [joe] => 43 )
c) Array ( [PETER] => 35 [BEN] => 37 [JOE] => 43 )
d) Array ( [PeTeR] => 35 [BeN] => 37 [Joe] => 43 )

Answer: c
Explanation: The array_change_key_case() function changes all keys in an array to lowercase or uppercase.

3. What will be the output of the following PHP code?
<?php
$cars = array("Volvo", "BMW", "Toyota", "Honda", "Mercedes", "Opel");
print_r(array_chunk($cars, 2));
?>
a) Array ( [0] => Array ( [1] => Volvo [2] => BMW ) [1] => Array ( [1] => Toyota [2] => Honda ) [2] => Array ( [1] => Mercedes [2] => Opel ) )
b) Array ( [1] => Array ( [1] => Volvo [2] => BMW ) [2] => Array ( [1] => Toyota [2] => Honda ) [3] => Array ( [1] => Mercedes [2] => Opel ) )
c) Array ( [0] => Array ( [0] => Volvo [1] => Volvo ) [1] => Array ( [0] => BMW [1] => BMW ) [2] => Array ( [0] => Toyota [1] => Toyota ) )
d) Array ( [0] => Array ( [0] => Volvo [1] => BMW ) [1] => Array ( [0] => Toyota [1] => Honda ) [2] => Array ( [0] => Mercedes [1] => Opel ) )

Answer: d
Explanation: The array_chunk() function splits an array into chunks of new arrays.

4. What will be the output of the following PHP code?
<?php
$fname = array("Peter", "Ben", "Joe");
$age = array("35", "37", "43");
$c = array_combine($fname, $age);
print_r($c);
?>
a) Array ( Peter Ben Joe )
b) Array ( [Peter] => 35 [Ben] => 37 [Joe] => 43 )
c) Array ( 35 37 43 )
d) Array ( “[Peter] => 35” “[Ben] => 37” “[Joe] => 43” )

Answer: b
Explanation: The array_combine() function creates an array by using the elements from one “keys” array and one “values” array.

5. What will be the output of the following PHP code?
<?php
$a = array("A", "Cat", "Dog", "A", "Dog");
print_r(array_count_values($a));
?>
a) Array ( [A] => 2 [Cat] => 1 [Dog] => 2 )
b) Array ( [A] => 2 [Cat] => 2 [Dog] => 1 )
c) Array ( [A] => 1 [Cat] => 1 [Dog] => 2 )
d) Array ( [A] => 2 [Cat] => 1 [Dog] => 1)

Answer: a
Explanation: The array_count_values() function counts all the values of an array.

6. 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");
$result = array_diff($a1, $a2);
print_r($result);
?>
a) Array ( [d] => yellow )
b) Array ( [c] => blue )
c) Array ( [a] => red )
d) Array ( [e] => yellow )

Answer: a
Explanation: The array_diff() function compares the values of two (or more) arrays, and returns the differences.

7. What will be the output of the following PHP code?
<?php
$a1 = array_fill(3, 4, "blue");
$b1 = array_fill(0, 1, "red");
print_r($a1);
echo "<br>";
print_r($b1);
?>
a) Array ( [3] => blue [4] => blue)
Array ( [0] => red )
b) Array ( [4] => blue [5] => blue [6] => blue)
Array ( [0] => red )
c) Array ( [3] => blue [4] => blue [5] => blue [6] => blue )
Array ()
d) Array ( [3] => blue [4] => blue [5] => blue [6] => blue )
Array ( [0] => red )

Answer: d
Explanation: The array_fill() function fills an array with values.

8. What will be the output of the following PHP code?
<?php
$a1 = array("red", "green");
$a2 = array("blue", "yellow");
print_r(array_merge($a1, $a2));
?>
a) Array ( [0] => red [1] => green)
b) Array ( [0] => blue [1] => yellow [2] => red [3] => green )
c) Array ( [0] => red [1] => green [2] => blue [3] => yellow )
d) Array ( [0] => blue [1] => yellow )

Answer: c
Explanation: The array_merge() function merges one or more arrays into one array.

9. What will be the output of the following PHP code?
<?php
$a = array("a"=>"red", "b"=>"green", "c"=>"blue");
echo array_shift($a);
print_r ($a);
?>
a) green
b) red
c) redArray( [c] => green [c] => blue )
d) redArray( [b] => green [c] => blue )

Answer: d
Explanation: The array_shift() function removes the first element from an array, and returns the value of the removed element.

10. What will be the output of the following PHP code?
<?php
$a = array("red", "green", "blue");
array_pop($a);
print_r($a);
?>
a) Array ( [0] => red [1] => green )
b) Array ( [0] => green [1] => blue )
c) Array ( [0] => red [1] => blue )
d) Array ( [0] => blue [1] => blue )

Answer: a
Explanation: The array_pop() function deletes the last element of an array.