PHP Top Ten Arrays MCQ Questions & Answers – 1

This set of PHP top ten  multiple choice mcq questions and answers focuses on arrays. It will be useful for anyone learning Fundamentals and PHP Basics.

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

<?php
 $state = array ("Karnataka", "Goa", "Tamil Nadu",
 "Andhra Pradesh");
 echo (array_search ("Tamil Nadu", $state) );
 ?>

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

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

2. Which in-built function will add a value to the end of an array?

a) array_unshift()
b) array_push()
c) inend_array()
d) into_array()

Answer: b
Explanation: array_push adds a value to the end of an array, returning the total count of elementsin the array after the new value has been added.

3. Which function will return true if a variable is an array or false if it is not?

a) this_array()
b) in_array()
c) do_array()
d) is_array()

Explanation: A built-in function, is_array(), is available for testing an array. Its prototype follows: boolean is_array(mixed variable).

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

<?php
 $fruits = array ("apple", "orange", "banana");
 echo (next($fruits));
 echo (next($fruits));
 ?>

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

Answer: c
Explanation: The next() function returns the array value residing at the position immediately following that of the current array pointer.

5. Which function can be used to move the pointer to the previous array position?
a) last()
b) before()
c) previous()
d) prev()

Answer: d
Explanation:None.

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

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

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

Answer: a
Explanation:The array entity holding pear and mango is counted as an item, just as its contents are.

7. Which function returns an array consisting of associative key/value pairs?

a) count()
b) array_count_values()
c) array_count()
d) count_values()

Answer: b
Explanation:None.
Sanfoundry Global Education &

8. PHP’s numerically indexed array begin with position __.

a) 0
b) 1
c) 2
d) -1

Answer: a
Explanation: Like many programming languages, the first element of an array has an index value of 0.

9. 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)
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.

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

<?php
 $states = array("karnataka" => array
 ( "population" => "11,35,000", "captial" => "Bangalore"),
 "Tamil Nadu" => array( "population" => "17,90,000",
 "captial" => "Chennai") );
 echo $states["karnataka"]["population"];
 ?>

a) 11,35,000
b) karnataka 11,35,000
c) population 11,35,000
d) karnataka population

Answer: a
Explanation: Treat states as a multidimensional array and accordingly traverse it to get the value.

PHP Top Ten Arrays MCQ Questions & Answers –2

PHP MCQ part 2, I will introduce best impotent  Ten Arrays MCQ Questions & Answers…