PHP Array MCQs
PHP Array MCQs : This section focuses on "Array" in PHP. These Multiple Choice Questions (mcq) should be practiced to improve the PHP skills required for various interviews (campus interview, walk-in interview, company interview), placements, entrance exams and other competitive examinations.
1. How many types of array are available in php?
A. 1
B. 2
C. 3
D. 4
View Answer
Ans : C
Explanation: There are basically three types of arrays in PHP: Indexed or Numeric Arrays, Associative Arrays, Multidimensional Arrays
2. By default, the index of array in php starts from ______?
A. 0
B. 1
C. -1
D. 2
View Answer
Ans : A
Explanation: By default, the index starts from zero.
3. A ________ is an array with more than two dimensions.
A. single dimensional array
B. multi dimensional array
C. Both A and B
D. None of the above
View Answer
Ans : B
Explanation: A multidimensional array is an array with more than two dimensions. In a matrix, the two dimensions are represented by rows and columns
4. Which of the following are correct ways of creating an array?
i) arr[0] = "letsfindcourse";
ii) $arr[] = array("letsfindcourse");
iii) $arr[0] = "letsfindcourse";
iv) $arr = array("letsfindcourse");
A. ii), iii) and iv)
B. ii) and iii)
C. Only i)
D. iii) and iv)
View Answer
Ans : D
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.
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
Ans : D
Explanation: array_push adds value to the end of an array, returning the total count of elements in the array after the new value has been added.
6. 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
Ans : C
Explanation: The function array_count_values() will count all the values of an array.
7. What will be the output of the following PHP code?
<?php
$arr = array ("lets", "find", "course", ".Com");
echo (array_search (".com", $arr) );
?>
A. 0
B. Garbage value
C. 3
D. No Output
View Answer
Ans : D
Explanation: Due to case-sensitive .com is not in an array so there will be no output.
8. As compared to associative arrays vector arrays are much
A. Faster
B. Slower
C. Stable
D. None of the above
View Answer
Ans : A
Explanation: As compared to associative arrays vector arrays are much Faster.
9. What is the output of the following php code?
<?php
$alphabet = array ("A", "B", "C");
echo (next($alphabet));
?>
A. A
B. B
C. C
D. Error
View Answer
Ans : B
Explanation: The next function is an in-built function used to print the next value of the current index. As the current index value is 0 so next will print value B.
10. What is the output of the following php code?
<?php
$alphabet = array("A" => array
( "php" => "php 7.0", "date" => "3 December 2019"),
"B" => array( "python" => "python 3.8.2",
"date" => "24 December 2019") );
echo $alphabet ["A"]["date"];
?>
A. php 7.0
B. 3 December 2019
C. python 3.8.2
D. 24 December 2019
View Answer
Ans : B
Explanation: The output of the following PHP code is 3 December 2015.
11. Keys in array can be non-sequential.
A. True
B. False
C. Depend on program
D. Keys does not exist in array
View Answer
Ans : A
Explanation: Keys in an array can be non-sequential.
12. Key/value pair can be initialized using = operator?
A. True
B. False
C. Depend on program
D. Keys does not exist in array
View Answer
Ans : A
Explanation: Key/value pair can be initialized using = operator
13. What is the use of is_array() function?
A. Function is used to print the array in readable format.
B. Adds elements to the beginning of the array and returns the size of array.
C. Function can be used to verify if a variable is an array. Returns TRUE or FALSE
D. Adds elements to the end of the array and returns the size of array.
View Answer
Ans : C
Explanation: is_array() Function can be used to verify if a variable is an array. Returns TRUE or FALSE.
14. What is the use of array_unshift() function?
A. Function is used to print the array in readable format.
B. Adds elements to the beginning of the array and returns the size of array.
C. Function can be used to verify if a variable is an array. Returns TRUE or FALSE
D. Adds elements to the end of the array and returns the size of array.
View Answer
Ans : B
Explanation: array_unshift() Adds elements to the beginning of the array and returns the size of array.
15. What is the use of array_flip() function?
A. Rearranges the array elements in the reverse order
B. Is used to convert the keys to values and values to keys.
C. Can be used to fetch the keys present in the array
D. Returns number of elements in the array
View Answer
Ans : B
Explanation: array_flip() Is used to convert the keys to values and values to keys.
16. What is the use of array_values() function?
A. Returns all the values of the respective keys present in the passed array
B. Returns number of elements in the array
C. Can be used to fetch the keys present in the array
D. Returns the values and the frequency of each value in form of an array.
View Answer
Ans : D
Explanation: array_count_values() Returns the values and the frequency of each value in form of an array.
17. Predict the output of the following code.
<?php
$a=array(1,2,3,5,6);
next($a);
next($a);
next($a);
echo current($a);
?>
A. 2
B. 3
C. 4
D. 5
View Answer
Ans : D
Explanation: 5 is the output of the following code.
18. Predict the output of the following PHP code.
<?php
$fruits = array ("apple", "orange", "banana", "guava", "pine-apple", "papaya", "melon");
next($fruits);
next($fruits);
prev($fruits);
?>
A. guava
B. banana
C. orange
D. pine-apple
View Answer
Ans : C
Explanation: orange be the output of the following PHP code.
19. 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
Ans : C
Explanation: The prev() function returns the previous element in the array.
20. Which of the following function is Used to set the array pointer to the value of last key?
A. last()
B. end()
C. next()
D. final()
View Answer
Ans : B
Explanation: The end() function is Used to set the array pointer to the value of last key.
Also check :
Discussion