PHP Coding Questions and Answers – Arrays – 2

This set of Tricky PHP Questions & Answers focuses on “Arrays – 2”.

1. What will be the output of the following PHP code?

  1. <?php
  2. $cars = array("Volvo", "BMW", "Toyota");
  3. echo "I like " . $cars[2] . ", " . $cars[1] . " and " . $cars[0] . ".";
  4. ?>

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

Answer: d
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’.
advertisement
advertisement

2. What will be the output of the following PHP code?

  1. <?php
  2. $fname = array("Peter", "Ben", "Joe");
  3. $age = array("35", "37", "43");
  4. $c = array_combine($age, $fname);
  5. print_r($c);
  6. ?>

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

Answer: d
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.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

3. What will be the output of the following PHP code?

advertisement
  1. <?php
  2. $a=array("A","Cat","Dog","A","Dog");
  3. $b=array("A","A","Cat","A","Tiger");
  4. $c=array_combine($a,$b);
  5. print_r(array_count_values($c));
  6. ?>

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

Answer: d
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.
advertisement

4. What will be the output of the following PHP code?

  1. <?php
  2. $a1 = array("a" => "red", "b" => "green", "c" => "blue", "d" => "yellow");
  3. $a2 = array("e" => "red", "f" => "green", "g" => "blue", "h" => "orange");
  4. $a3 = array("i" => "orange");
  5. $a4 = array_merge($a2, $a3);
  6. $result = array_diff($a1, $a4);
  7. print_r($result);
  8. ?>

a) Array ( [d] => yellow )
b) Array ( [i] => orange )
c) Array ( [h] => orange )
d) Array ( [d] => yellow [h] => orange )
View Answer

Answer: a
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?

  1. <?php
  2. $a1 = array("red", "green");
  3. $a2 = array("blue", "yellow");
  4. $a3 = array_merge($a1, $a2);
  5. $a4 = array("a", "b", "c", "d");
  6. $a = array_combine($a4, $a3);
  7. print_r($a);
  8. ?>

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

Answer: d
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?

  1. <?php
  2. $a = array("a" => "india", "b" => "brazil", "c" => "china");
  3. echo array_shift($a);
  4. echo "<br>";
  5. array_pop($a);
  6. print_r($a);
  7. ?>

a)

india
Array ( [b] => Brazil )

b)

india
Array ( [a] => brazil )

c)

china
Array ( [a] => india )

d)

china
Array ( [a] => brazil )
View Answer
Answer: a
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?

  1. <?php
  2. $a1 = array_fill(1, 4, "hello");
  3. $b1 = array_fill(5, 1, "php");
  4. $a2 = array_merge($a1, $a2);
  5. print_r($a2);
  6. echo "<br>";
  7. print_r($b1);
  8. ?>

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
Answer: c
Explanation: Usage of array_fill() and array_merge() functions.
 
 

8. What will be the output of the following PHP code?

  1. <?php
  2. $names = array("Sam", "Bob", "Jack");
  3. echo $names[0] . "is the brother of " . $names[1] . " and " . $names[1] . ".";
  4. ?>

a) Sam is the brother of Bob and Jack
b) Samis the brother of Bob and Bob
c) Sam is the brother of Jack and Bob
d) Error
View Answer

Answer: b
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?

  1. <?php
  2. $names = array("Sam", "Bob", "Jack");
  3. echo $names[0]."is the brother of ".$names[1]." and ".$names[1].".".$brother;
  4. ?>

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

Answer: d
Explanation: $brother undeclared.

10. What will be the output of the following PHP code?

  1. <?php
  2. $place = array("NYC", "LA", "Paris");
  3. array_pop($place);
  4. $place1 = array("Paris");
  5. $place = array_merge($place, $place1);
  6. print_r($place);
  7. ?>

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

Answer: b
Explanation: array_merge() and array_pop() yields that result.

Sanfoundry Global Education & Learning Series – PHP Programming.

To practice all tricky questions on PHP, here is complete set of 1000+ Multiple Choice Questions and Answers on PHP.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.