PHP Coding Questions and Answers – Arrays – 4

This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “Arrays – 4”.

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

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

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)
View Answer

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

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

  1. <?php
  2. $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
  3. print_r(array_change_key_case($age, CASE_UPPER));
  4. ?>

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 )
View Answer

Answer: c
Explanation: The array_change_key_case() function changes all keys in an array to lowercase or uppercase.
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. $cars = array("Volvo", "BMW", "Toyota", "Honda", "Mercedes", "Opel");
  3. print_r(array_chunk($cars, 2));
  4. ?>

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 ) )
View Answer

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

4. 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($fname, $age);
  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 ( “[Peter] => 35” “[Ben] => 37” “[Joe] => 43” )
View Answer

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?

  1. <?php
  2. $a = array("A", "Cat", "Dog", "A", "Dog");
  3. print_r(array_count_values($a));
  4. ?>

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)
View Answer

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?

  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_diff($a1, $a2);
  5. print_r($result);
  6. ?>

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

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?

  1. <?php
  2. $a1 = array_fill(3, 4, "blue");
  3. $b1 = array_fill(0, 1, "red");
  4. print_r($a1);
  5. echo "<br>";
  6. print_r($b1);
  7. ?>

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 )
View Answer
Answer: d
Explanation: The array_fill() function fills an array with values.
 
 

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

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

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 )
View Answer

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?

  1. <?php
  2. $a = array("a"=>"red", "b"=>"green", "c"=>"blue");
  3. echo array_shift($a);
  4. print_r ($a);
  5. ?>

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

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?

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

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

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

Sanfoundry Global Education & Learning Series – PHP Programming.

To practice all questions on PHP Programming, 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.