PHP Multiple Choice Questions & Answers (MCQs) focuses on “Array”.
Q 1. PHP’s numerically indexed array begin with position ___________
A. 0
B. 1
C. 2
D. -1
Show Answer
Answer:-A. 0Explanation
Like all the other programming languages, the first element of an array always starts with ‘0’.Q 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. Only i)
B. ii) and iii)
C. iii) and iv)
D. ii), iii) and iv)
Show Answer
Answer:-C. 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.Q 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. 11,35,000
B. Karnataka 11,35,000
C. population 11,35,000
D. Karnataka population
Show Answer
Answer:-A. 11,35,000Explanation
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’.Q 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. do_array()
C. is_array()
D. in_array()
Show Answer
Answer:-C. is_array()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).Q 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()
Show Answer
Answer:-D. array_push()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.Q 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. 1
B. 2
C. False
D. True
Show Answer
Answer:-B. 2Explanation
The array_search() function searches an array for a specified value, returning its key if located and FALSE otherwise.Q 7. 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
Show Answer
Answer:-C. orangebananaExplanation
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.Q 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()
Show Answer
Answer:-C. prev()Explanation
The prev() function returns the previous element in the array.Q 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
Show Answer
Answer:-D. 6Explanation
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.Q 10. Which function returns an array consisting of associative key/value pairs?
A. array_count_values()
B. array_count()
C. count()
D. count_values()
Leave a Reply