PHP Coding Questions and Answers – Arrays – 3

This set of Tough PHP Questions & Answers focuses on “Arrays – 3”.

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

  1. <?php
  2.  $age = array("Harry" => "21", "Ron" => "23","Malfoy" => "21");
  3.  array_pop($age);
  4.  print_r(array_change_key_case($age, CASE_UPPER));
  5. ?>

a) Array ( [Harry] => 21 [Ron] => 23 [Malfoy] => 21 )
b) Array ( [HARRY] => 21 [RON] => 23 [MALFOY] => 21 )
c) Array ( [HARRY] => 21 [RON] => 23 )
d) Array ( [Harry] => 21 [Ron] => 23 )
View Answer

Answer: c
Explanation: The function array_pop() will delete the last element of an array. So Malfoy => 21 will be deleted and the function array_change_key_case() will change all keys in an array to lowercase or uppercase.
advertisement
advertisement

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

  1. <?php
  2. $a1 = array("a" => "red", "b" => "green", "c" => "blue", "d" => "yellow");
  3. $result = array_flip($a1);
  4. print_r($result);
  5. ?>

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

Answer: c
Explanation: The function array_flip() flips/exchanges all keys with their associated values in an array. So, in the above program “a” will be flipped with “red”, “b” will be flipped with “green” and so on.
Note: Join free Sanfoundry classes at Telegram or Youtube

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

advertisement
  1. <?php
  2. $a1 = array("a" => "red", "b" => "green", "c" => "blue", "d" => "yellow");
  3. $a2 = array("e" => "red","f" => "green", "g" => "blue");
  4. $result = array_intersect($a1, $a2);
  5. print_r($result);
  6. ?>

a) Array ( [a] => red [b] => green [c] => blue )
b) Array ( [a] => red [b] => green [c] => blue [d] => yellow )
c) Array ( [e] => red [f] => green [g] => blue )
d) Array ( [a] => red [b] => green [c] => blue [d] => yellow [e] => red [f] => green [g] => blue )
View Answer

Answer: a
Explanation: The function array_intersect() compares the values of two (or more) arrays, and returns the matches. So, in the above program values of a1 and a2 will be compared and the values present in both the arrays will be the returned.
advertisement

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

  1. <?php
  2. $a = array(12, 5, 2);
  3. echo(array_product($a));
  4. ?>

a) 024
b) 120
c) 010
d) 060
View Answer

Answer: b
Explanation: The array_product() function calculates and returns the product of an array.

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

  1. <?php
  2. $a = array("a" => "Jaguar", "b" => "Land Rover", 
  3. "c" => "Audi", "d" => "Maseratti");
  4. echo array_search("Audi", $a);
  5. ?>

a) a
b) b
c) c
d) d
View Answer

Answer: c
Explanation: The array_search() function searches for the element and returns the key of that element.

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

  1. <?php
  2. $city_west = array("NYC", "London");
  3. $city_east = array("Mumbai", "Beijing");
  4. print_r(array_replace($city_west, $city_east));
  5. ?>

a) Array ( [1] => Mumbai [0] => Beijing )
b) Array ( [0] => NYC [1] => London )
c) Array ( [1] => NYC [0] => London )
d) Array ( [0] => Mumbai [1] => Beijing )
View Answer

Answer: d
Explanation: The function array_replace() replaces the values of the first array with the values from following arrays. So, in the above program the values of city_west will be replaced with city_east.

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

  1. <?php
  2. $people = array("Peter", "Susan", "Edmund", "Lucy");
  3. echo pos($people);
  4. ?>

a) Lucy
b) Peter
c) Susan
d) Edmund
View Answer

Answer: b
Explanation: The pos() function returns the value of the current element in an array, and since no operation has been done, the current element is the first element.

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

  1. <?php
  2. $number = range(0, 5);
  3. print_r ($number);
  4. ?>

a) Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 )
b) Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 )
c) Array ( [0] => 5 [1] => 5 [2] => 5 [3] => 5 [4] => 5 [5] => 5 )
d) Array ( [0] => 0 [5] => 5 )
View Answer

Answer: a
Explanation: The range() function creates an array containing a range of elements.

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

  1. <?php
  2. $array = array("red", "green");
  3. array_push($array, "blue", "yellow");
  4. print_r($array);
  5. ?>

a) Array ( [0] => red [1] => green [2] => blue [3] => yellow )
b) Array ( [0] => blue [1] => yellow [2] => red [3] => green )
c) Array ( [0] => red [1] => green )
d) Array ( [0] => blue [1] => yellow )
View Answer

Answer: a
Explanation: The function array_push() inserts one or more elements to the end of an array. So, in the above program blue and yellow will be inserted after previous values.

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

  1. <?php
  2. $age = array("Harry" => "21", "Ron" => "19", "Malfoy" => "23");
  3. ksort($age);
  4. foreach($age as $x => $x_value)
  5. {
  6.     echo "Key=" . $x . ", Value=" . $x_value;
  7.     echo "<br>";
  8. }
  9. ?>

a)

  Key = Harry, Value = 21
  Key = Ron, Value = 21
  Key = Malfoy, Value = 23

b)

  Key = Harry, Value = 21
  Key = Ron, Value = 19
  Key = Malfoy, Value = 23

c)

  Key = Harry, Value = 21
  Key = Malfoy, Value = 23
  Key = Ron, Value = 19

d)

  Key = Ron, Value = 19
  Key = Harry, Value = 21
  Key = Malfoy, Value = 23
View Answer
Answer: c
Explanation: The ksort() function sorts an associative array in ascending order, according to the key.
 
 

Sanfoundry Global Education & Learning Series – PHP Programming.

To practice all tough 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.