Arrays - Study Mode

[#31] 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 ))
?>
Correct Answer

(A) Array ( [A] => 5 [Cat] => 2 [Dog] => 2 [Tiger] => 1 )

Explanation

Solution: The array_count_values() function counts all the values of an array and array_combine() combines arrays.

[#32] 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_combine ( $a2, $a3 )
$result = array_diff ( $a4, $a2 )
print_r ( $result )
?>
Correct Answer

(A) Array ( [d] => yellow )

Explanation

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

[#33] 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 )
?>
Correct Answer

(D) Array ( [a] => red [b] => green [c] => blue [d] => yellow )

Explanation

Solution: Basic combined application of array_combine() and array_merge().

[#34] 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 ] . "."
?>
Correct Answer

(B) Sam is the brother of Bob and Bob)

Explanation

Solution: Simple definition of array and using it in a string. We have used $names[1] twice and hence Bob appears twice.

[#35] 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
?>
Correct Answer

(D) Error

Explanation

Solution: $brother undeclared.