PHP Questions and Answers Part-8

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

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)

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.

3. What will be the output of the following PHP code?
<?php
$states = array("Karnataka" => array
("population" => "11,35,000", "capital" => "Bangalore"),
"Tamil Nadu" => array( "population" => "17,90,000",
"capital" => "Chennai") );
echo $states["Karnataka"]["population"];
?>
a) Karnataka 11,35,000
b) 11,35,000
c) population 11,35,000
d) Karnataka population

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’.

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()

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()

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.

6. 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) False
d) 2

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?
<?php
$fruits = array ("apple", "orange", "banana");
echo (next($fruits));
echo (next($fruits));
?>
a) orangebanana
b) appleorange
c) orangeorange
d) appleapple

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()

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

9. What will be the output of the following PHP code?
<?php
$fruits = array ("apple", "orange", array ("pear", "mango"),
"banana");
echo (count($fruits, 1));
?>
a) 3
b) 4
c) 5
d) 6

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()

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.