PHP Questions & Answers – Arrays – 5

This set of PHP Questions and Answers focuses on “Arrays – 5”.

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

  1.     <?php
  2.     $fruits = array ("mango", "apple", "pear", "peach");
  3.     $fruits = array_flip($fruits);
  4.     echo ($fruits[0]);
  5.     ?>

a) mango
b) error
c) peach
d) 0
View Answer

Answer: b
Explanation: As we are flipping the values, $fruits[“mango”] = 0, $fruits[“apple”] = 1 and so on.
advertisement
advertisement

2. Which of the functions is used to sort an array in descending order?
a) sort()
b) asort()
c) rsort()
d) dsort()
View Answer

Answer: c
Explanation: The function sort() will sort the arrays in ascending order, the function rsort() will sort arrays in descending order. While the function asort() will sort associative arrays in ascending order, according to the value.

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

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
  1.     <?php
  2.     $fruits = array ("mango", "apple", "peach", "pear");
  3.     $fruits = asort ($fruits);
  4.     printr ($fruits);
  5.     ?>

a) Array ( [1] => apple [0] => mango [2] => peach [3] => pear )
b) Array ( [0] => apple [1] => mango [2] => peach [3] => pear )
c) Error
d) Array ( [1] => apple [0] => mango [3] => peach [2] => pear )
View Answer

Answer: c
Explanation: The program will give an error i.e. uncaught error: call to undefined function printr(). As the correct function is print_r().
advertisement

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

  1.     <?php
  2.     $arr = array ("picture1.JPG", "picture2.jpg",
  3.     "Picture10.jpg", "picture20.jpg");
  4.     sort($arr);
  5.     print_r($arr);
  6.     ?>

a) Array ( [0] => picture1.JPG [1] => Picture10.jpg [2] => picture2.jpg [3] => picture20.jpg )
b) Array ( [0] => picture1.JPG [1] => picture2.jpg [2] => Picture10.jpg [3] => picture20.jpg )
c) Array ( [0] => Picture10.jpg [1] => picture1.JPG [2] => picture2.jpg [3] => picture20.jpg )
d) Array ( [0] => Picture10.jpg [1] => picture1.JPG [2] => picture20.jpg [3] => picture2.jpg )
View Answer

Answer: c
Explanation: The function sort() in PHP sorts an indexed array in ascending order. While sorting, each character is compared with the others and sorted using ASCII values.
advertisement

5. Which function should we use to sort the array in natural order?
a) dsort()
b) casesort()
c) natcasesort()
d) naturalsort()
View Answer

Answer: c
Explanation: The function natcasesort() in PHP sorts an array by using a “natural order” algorithm. All the values keep their original keys. Eg: In a natural algorithm, as the number 2 is less than the number 10. But in computer sorting, 10 is less than 2, because the first number in “10” is less than 2. The function is case-insensitive.

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

  1.     <?php
  2.     $face = array ("A", "J", "Q", "K");
  3.     $number = array ("2","3","4", "5", "6", "7", "8", "9", "10");
  4.     $cards = array_merge ($face, $number);
  5.     print_r ($cards);
  6.     ?>

a) Array ( [0] => A [1] => J [2] => Q [3] => K [4] => 2 [5] => 3 [6] => 4 [7] => 5 [8] => 6 [9] => 7 [10] => 8 [11] => 9 [12] => 10 )
b) Array ( [0] => A [1] => 2 [2] => J [3] => 3 [4] => Q [5] => 4 [6] => K [7] => 5 [8] => 6 [9] => 7 [10] => 8 [11] => 9 [12] => 10 )
c) Error
d) Array ( [0] => 2 [1] => 3 [2] => 4 [3] => 5 [4] => 6 [5] => 7 [6] => 8 [7] => 9 [8] => 10 [9] => A [10] => J [11] => Q [12] => K )
View Answer

Answer: a
Explanation: The array_merge() function merges one or more arrays into one array. The resulting array will begin with the first input array parameter, appending each subsequent array parameter in the order of appearance.

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

  1.     <?php
  2.     $fruits = array ("apple", "mango", "peach", "pear",
  3.     "orange");
  4.     $subset = array_slice ($fruits, 2);
  5.     print_r ($subset);
  6.     ?>

a) Array ( [0] => peach )
b) Array ( [0] => apple [1] => mango [2] => peach )
c) Array ( [0] => apple [1] => mango )
d) Array ( [0] => peach [1] => pear [2] => orange )
View Answer

Answer: d
Explanation: The array_slice() function returns a section of an array based on a starting and ending offset value.

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

  1.     <?php
  2.     $fruits = array ("apple", "mango", "peach", "pear",
  3.     "orange");
  4.     $subset = array_splice ($fruits, 2);
  5.     print_r ($fruits);
  6.     ?>

a) Error
b) Array ( [0] => apple [1] => mango [2] => peach )
c) Array ( [0] => apple [1] => mango )
d) Array ( [0] => pear [1] => orange )
View Answer

Answer: c
Explanation: The array_splice() function removes all elements of an array found within a specified range.

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

  1.     <?php
  2.     $number = array ("4", "hello", 2);
  3.     echo (array_sum ($number));
  4.     ?>

a) 4hello2
b) 4
c) 2
d) 6
View Answer

Answer: d
Explanation: The array_sum() function add all the values of the input array together, returning the final sum. If a string datatype is found, it’ll be ignored.

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

  1.     <?php
  2.     $array1 = array ("KA", "LA", "CA", "MA", "TA");
  3.     $array2 = array ("KA", "IA", "CA", "GA", "TA");
  4.     $inter = array_intersect ($array1, $array2);
  5.     print_r ($inter);
  6.     ?>

a) Array ( [0] => KA [1] => LA [2] => CA [3] => MA [4] => TA [5] => IA [6] => GA )
b) Array ( [0] => KA [2] => CA [4] => TA )
c) Array ( [1] => IA [3] => GA )
d) Array ( [1] => LA [3] => MA )
View Answer

Answer: b
Explanation: The array_intersect() function returns a key preserved array consisting only of those values present in the first array that are also present in each of the other input arrays.

Sanfoundry Global Education & Learning Series – PHP Programming.

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