PHP Multiple Choice Questions – Array

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

1. PHP’s numerically indexed array begin with position ___________
a) 1
b) 2
c) 0
d) -1
View Answer

Answer: c
Explanation: Like all the other programming languages, the first element of an array always starts with ‘0’.

2. Which of the following are correct ways of creating an array?

i) state[0] = "karnataka";
ii) $state[] = array("karnataka");
iii) $state[0] = "karnataka";
iv)  $state = array("karnataka");

a) iii) and iv)
b) ii) and iii)
c) Only i)
d) ii), iii) and iv)
View Answer

Answer: a
Explanation: A variable name should start with $ symbol which is not present in i) and you need not put the square brackets when you use the array() constructor.
advertisement
advertisement

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

  1.     <?php
  2.     $states = array("Karnataka" => array
  3.     ("population" => "11,35,000", "capital" => "Bangalore"),
  4.     "Tamil Nadu" => array( "population" => "17,90,000",
  5.     "capital" => "Chennai") );
  6.     echo $states["Karnataka"]["population"];
  7.     ?>

a) Karnataka 11,35,000
b) 11,35,000
c) population 11,35,000
d) Karnataka population
View Answer

Answer: b
Explanation: In the following PHP code, the variable states are treated as a multidimensional array and accordingly traverse it to get the value of ‘Karnataka’s population’.
advertisement

4. Which of the following PHP function will return true if a variable is an array or false if it is not an array?
a) this_array()
b) is_array()
c) do_array()
d) in_array()
View Answer

Answer: b
Explanation: The function is_array() is an inbuilt function in PHP which is used to check whether a variable is an array or not. Its prototype follows: boolean is_array(mixed variable).

5. Which in-built function will add a value to the end of an array?
a) array_unshift()
b) into_array()
c) inend_array()
d) array_push()
View Answer

Answer: d
Explanation: array_push adds a value to the end of an array, returning the total count of elements in the array after the new value has been added.
advertisement

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

  1.     <?php
  2.     $state = array ("Karnataka", "Goa", "Tamil Nadu",
  3.     "Andhra Pradesh");
  4.     echo (array_search ("Tamil Nadu", $state) );
  5.     ?>

a) True
b) 1
c) False
d) 2
View Answer

Answer: d
Explanation: The array_search() function searches an array for a specified value, returning its key if located and FALSE otherwise.

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

  1.     <?php
  2.     $fruits = array ("apple", "orange", "banana");
  3.     echo (next($fruits));	
  4.     echo (next($fruits));
  5.     ?>

a) orangebanana
b) appleorange
c) orangeorange
d) appleapple
View Answer

Answer: a
Explanation: The next() function returns the value of the next element in the array. In the first ‘next($fruits)’ call, it will print orange which is next to apple and so on.

8. Which of the following function is used to get the value of the previous element in an array?
a) last()
b) before()
c) prev()
d) previous()
View Answer

Answer: c
Explanation: The prev() function returns the previous element in the array.

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

  1.     <?php
  2.     $fruits = array ("apple", "orange", array ("pear", "mango"),
  3.     "banana");
  4.     echo (count($fruits, 1));
  5.     ?>

a) 3
b) 4
c) 5
d) 6
View Answer

Answer: d
Explanation: The function count() will return the number of elements in an array. The parameter 1 counts the array recursively i.e it will count all the elements of multidimensional arrays.

10. Which function returns an array consisting of associative key/value pairs?
a) count()
b) array_count()
c) array_count_values()
d) count_values()
View Answer

Answer: c
Explanation: The function array_count_values() will count all the values of an array. It will return an associative array, where the keys will be the original array’s values, and the values are the number of occurrences.

More MCQs on PHP 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.