PHP Coding Questions and Answers – Arrays – 2

 PHP Multiple Choice Questions & Answers (MCQs) focuses on “Arrays – 2”.

Q 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

Show Answer Answer:-D. I like Toyota, BMW and Volvo
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’.

Q 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)

Show Answer Answer:-D. Array ([35] => Peter [37] => Ben [43] => Joe)
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.

Q 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] => 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] => 5 [Cat] => 2 [Dog] => 2 [Tiger] => 1 )

Show Answer Answer:-A. Array ( [A] => 2 [Tiger] => 1 )
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.

Q 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 )

Show Answer Answer:-A. Array ( [d] => yellow )
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).

Q 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 ( [a] => red [b] => green [c] => blue [d] => yellow )
C. Array ( [0] => red [1] => green [2] => blue [3] => yellow )
D. Array ( [0] => blue [1] => yellow [2] => red [3] => green )

Show Answer Answer:-B. Array ( [a] => red [b] => green [c] => blue [d] => yellow )
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().

Q 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 )

Show Answer Answer:-A. India Array ( [b] => Brazil )
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.

Q 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 )

Show Answer Answer:-C. Array ( [1] => hello [2] => hello [3] => hello [4] => hello [5] => php ) Array ( [5] => php )
Explanation Usage of array_fill() and array_merge() functions.

Q 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 Bob.
B. Sam is the brother of Bob and Jack.
C. Sam is the brother of Jack and Bob.
D. Error

Show Answer Answer:-A. Sam is the brother of Bob and Bob.
Explanation Simple definition of array and using it in a string. We have used $names[1] twice and hence Bob appears twice.

Q 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

Show Answer Answer:-D. Error
Explanation $brother undeclared.

Q 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] => LA [1] => Paris )
C. Array ( [0] => NYC [1] => LA [2] => Paris [3] => Paris )
D. Array ( [0] => NYC [1] => LA [2] => Paris)

Show Answer Answer:-D. Array ( [0] => NYC [1] => LA [2] => Paris)
Explanation Answer: b Explanation: array_merge() and array_pop() yields that result.


Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You cannot copy content of this page