R4RIN
MCQS
PHP MCQ Quiz Hub
PHP Mcq Arrays
Choose a topic to test your knowledge and improve your PHP skills
1. PHP’s numerically indexed array begin with position ___________
1
2
0
-1
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");
iii) and iv)
ii) and iii)
Only i)
ii), iii) and iv)
3. Which of the following PHP function will return true if a variable is an array or false if it is not an array?
this_array()
is_array()
do_array()
in_array()
4. Which in-built function will add a value to the end of an array?
array_unshift()
into_array()
inend_array()
array_push()
5. 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) ); ?>
True
1
False
2
6. What will be the output of the following PHP code? <?php $fruits = array ("apple", "orange", "banana"); echo (next($fruits)); echo (next($fruits)); ?>
orangebanana
appleorange
orangeorange
appleapple
7. Which of the following function is used to get the value of the previous element in an array?
last()
before()
prev()
previous()
8. What will be the output of the following PHP code? <?php $fruits = array ("apple", "orange", array ("pear", "mango"), "banana"); echo (count($fruits, 1)); ?>
3
4
5
6
9. Which function returns an array consisting of associative key/value pairs?
count()
array_count()
array_count_values()
count_values()
10. What will be the output of the following PHP code? <?php $cars = array("Volvo", "BMW", "Toyota"); echo "I like " . $cars[2] . ", " . $cars[1] . " and " . $cars[0] . "."; ?>
I like Volvo, Toyota and BMW
I like Volvo, BMW and Toyota
I like BMW, Volvo and Toyota
I like Toyota, BMW and Volvo
11. What will be the output of the following PHP code? <?php $fname = array("Peter", "Ben", "Joe"); $age = array("35", "37", "43"); $c = array_combine($age, $fname); print_r($c); ?>
Array (Peter Ben Joe)
Array ([Peter] =&gt; 35 [Ben] =&gt; 37 [Joe] =&gt; 43)
Array (35 37 43)
Array ([35] =&gt; Peter [37] =&gt; Ben [43] =&gt; Joe)
12. What will be the output of the following PHP code? <?php $a=array("A","Cat","Dog","A","Dog"); $b=array("A","A","Cat","A","Tiger"); $c=array_combine($a,$b); print_r(array_count_values($c)); ?>
Array ( [A] =&gt; 5 [Cat] =&gt; 2 [Dog] =&gt; 2 [Tiger] =&gt; 1 )
Array ( [A] =&gt; 2 [Cat] =&gt; 2 [Dog] =&gt; 1 [Tiger] =&gt; 1 )
Array ( [A] =&gt; 6 [Cat] =&gt; 1 [Dog] =&gt; 2 [Tiger] =&gt; 1 )
Array ( [A] =&gt; 2 [Tiger] =&gt; 1 )
13. What will be the output of the following PHP code? <?php $a1 = array("a" => "red", "b" => "green", "c" => "blue", "d" => "yellow"); $a2 = array("e" => "red", "f" => "green", "g" => "blue", "h" => "orange"); $a3 = array("i" => "orange"); $a4 = array_merge($a2, $a3); $result = array_diff($a1, $a4); print_r($result); ?>
Array ( [d] =&gt; yellow )
Array ( [i] =&gt; orange )
Array ( [h] =&gt; orange )
Array ( [d] =&gt; yellow [h] =&gt; orange )
14. What will be the output of the following PHP code? <?php $a1 = array("red", "green"); $a2 = array("blue", "yellow"); $a3 = array_merge($a1, $a2); $a4 = array("a", "b", "c", "d"); $a = array_combine($a4, $a3); print_r($a); ?>
Array ( [a] =&gt; blue [b] =&gt; yellow [c] =&gt; red [d] =&gt; green )
Array ( [0] =&gt; blue [1] =&gt; yellow [2] =&gt; red [3] =&gt; green )
Array ( [0] =&gt; red [1] =&gt; green [2] =&gt; blue [3] =&gt; yellow )
Array ( [a] =&gt; red [b] =&gt; green [c] =&gt; blue [d] =&gt; yellow )
15. What will be the output of the following PHP code? <?php $a = array("a" => "india", "b" => "brazil", "c" => "china"); echo array_shift($a); echo "<br>"; array_pop($a); print_r($a); ?>
india Array ( [b] =&gt; Brazil )
india Array ( [a] =&gt; brazil )
china Array ( [a] =&gt; india )
china Array ( [a] =&gt; brazil )
16. What will be the output of the following PHP code? <?php $a1 = array_fill(1, 4, "hello"); $b1 = array_fill(5, 1, "php"); $a2 = array_merge($a1, $a2); print_r($a2); echo "<br>"; print_r($b1); ?>
Array ( [1] =&gt; hello [4] =&gt; hello [5] =&gt; php ) Array ( [5] =&gt; php )
Array ( [1] =&gt; hello [2] =&gt; hello [3] =&gt; hello [4] =&gt; hello ) Array ( [5] =&gt; php )
Array ( [1] =&gt; hello [2] =&gt; hello [3] =&gt; hello [4] =&gt; hello [5] =&gt; php ) Array ( [5] =&gt; php )
Array ( [1] =&gt; hello [2] =&gt; hello [3] =&gt; hello [4] =&gt; hello ) Array ( [1] =&gt; php )
17. What will be the output of the following PHP code? <?php $names = array("Sam", "Bob", "Jack"); echo $names[0] . "is the brother of " . $names[1] . " and " . $names[1] . "."; ?>
Sam is the brother of Bob and Jack
Samis the brother of Bob and Bob
Sam is the brother of Jack and Bob
Error
18. What will be the output of the following PHP code? <?php $names = array("Sam", "Bob", "Jack"); echo $names[0]."is the brother of ".$names[1]." and ".$names[1].".".$brother; ?>
Sam is the brother of Bob and Bob) $brother
Sam is the brother of Bob and Bob)
$brother
Error
19. By default, the index of array in php starts from ______?
0
1
-1
2
20. A ________ is an array with more than two dimensions.
A. single dimensional array
multi dimensional array
Both A and B
None of the above
21. 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");
ii), iii) and iv)
ii) and iii)
Only i) D
iii) and iv)
22. Which in-built function will add a value to the end of an array?
array_unshift()
into_array()
inend_array()
array_push()
23. Which function returns an array consisting of associative key/value pairs?
count()
array_count()
array_count_values()
count_values()
24. What will be the output of the following PHP code? <?php $arr = array ("lets", "find", "course", ".Com"); echo (array_search (".com", $arr) ); ?>
0
Garbage value
3
No Output
25. As compared to associative arrays vector arrays are much
Faster
Slower
Stable
None of the above
26. What is the output of the following php code? <?php $alphabet = array ("A", "B", "C"); echo (next($alphabet)); ?>
A
B
C
Error
27. 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"]; ?>
php 7.0
3 December 2019
python 3.8.2
24 December 2019
28. Key/value pair can be initialized using = operator?
True
False
Depend on program
Keys does not exist in array
29. What is the use of is_array() function?
Function is used to print the array in readable format.
Adds elements to the beginning of the array and returns the size of array.
Function can be used to verify if a variable is an array. Returns TRUE or FALSE
Adds elements to the end of the array and returns the size of array.
30. What is the use of array_unshift() function?
Function is used to print the array in readable format.
Adds elements to the beginning of the array and returns the size of array
Function can be used to verify if a variable is an array. Returns TRUE or FALSE
Adds elements to the end of the array and returns the size of array.
31. What is the use of array_flip() function?
Rearranges the array elements in the reverse order
Is used to convert the keys to values and values to keys.
Can be used to fetch the keys present in the array
Returns number of elements in the array
32. What is the use of array_values() function?
Returns all the values of the respective keys present in the passed array
Returns number of elements in the array
Can be used to fetch the keys present in the array
Returns the values and the frequency of each value in form of an array.
33. Predict the output of the following code. <?php $a=array(1,2,3,5,6); next($a); next($a); next($a); echo current($a); ?>
2
3
4
5
34. 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); ?>
guava
banana
orange
pine-apple
35. Which of the following function is used to get the value of the previous element in an array?
last()
before()
prev()
previous()
36. Which of the following function is Used to set the array pointer to the value of last key?
last()
end()
next()
final()
37. What will be the output of the following PHP code? <?php $cars = array("Volvo", "BMW", "Toyota"); echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . "."; ?>
I like Volvo BMW and Toyota.
I like Volvo, BMW and Toyota)
I like Volvo, BMW and Toyota
I like. Volvo.,. BMW. and. Toyota)
38. What will be the output of the following PHP code? <?php $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43"); print_r(array_change_key_case($age, CASE_UPPER)); ?>
a) Array ( [Peter] =&gt; 35 [Ben] =&gt; 37 [Joe] =&gt; 43 )
Array ( [peter] =&gt; 35 [ben] =&gt; 37 [joe] =&gt; 43 )
Array ( [PETER] =&gt; 35 [BEN] =&gt; 37 [JOE] =&gt; 43 )
Array ( [PeTeR] =&gt; 35 [BeN] =&gt; 37 [Joe] =&gt; 43 )
39. What will be the output of the following PHP code? <?php $cars = array("Volvo", "BMW", "Toyota", "Honda", "Mercedes", "Opel"); print_r(array_chunk($cars, 2)); ?>
Array ( [0] =&gt; Array ( [1] =&gt; Volvo [2] =&gt; BMW ) [1] =&gt; Array ( [1] =&gt; Toyota [2] =&gt; Honda ) [2] =&gt; Array ( [1] =&gt; Mercedes [2] =&gt; Opel ) )
Array ( [1] =&gt; Array ( [1] =&gt; Volvo [2] =&gt; BMW ) [2] =&gt; Array ( [1] =&gt; Toyota [2] =&gt; Honda ) [3] =&gt; Array ( [1] =&gt; Mercedes [2] =&gt; Opel ) )
Array ( [0] =&gt; Array ( [0] =&gt; Volvo [1] =&gt; Volvo ) [1] =&gt; Array ( [0] =&gt; BMW [1] =&gt; BMW ) [2] =&gt; Array ( [0] =&gt; Toyota [1] =&gt; Toyota ) )
Array ( [0] =&gt; Array ( [0] =&gt; Volvo [1] =&gt; BMW ) [1] =&gt; Array ( [0] =&gt; Toyota [1] =&gt; Honda ) [2] =&gt; Array ( [0] =&gt; Mercedes [1] =&gt; Opel ) )
40. What will be the output of the following PHP code? <?php $fname = array("Peter", "Ben", "Joe"); $age = array("35", "37", "43"); $c = array_combine($fname, $age); print_r($c); ?>
Array ( Peter Ben Joe )
Array ( [Peter] =&gt; 35 [Ben] =&gt; 37 [Joe] =&gt; 43 )
Array ( 35 37 43 )
Array ( “[Peter] =&gt; 35” “[Ben] =&gt; 37” “[Joe] =&gt; 43” )
41. What will be the output of the following PHP code? <?php $a = array("A", "Cat", "Dog", "A", "Dog"); print_r(array_count_values($a)); ?>
Array ( [A] =&gt; 2 [Cat] =&gt; 1 [Dog] =&gt; 2 )
Array ( [A] =&gt; 2 [Cat] =&gt; 2 [Dog] =&gt; 1 )
Array ( [A] =&gt; 1 [Cat] =&gt; 1 [Dog] =&gt; 2 )
Array ( [A] =&gt; 2 [Cat] =&gt; 1 [Dog] =&gt; 1)
42. What will be the output of the following PHP code? <?php $a1 = array_fill(3, 4, "blue"); $b1 = array_fill(0, 1, "red"); print_r($a1); echo "<br>"; print_r($b1); ?>
Array ( [3] =&gt; blue [4] =&gt; blue) Array ( [0] =&gt; red )
Array ( [4] =&gt; blue [5] =&gt; blue [6] =&gt; blue) Array ( [0] =&gt; red )
Array ( [3] =&gt; blue [4] =&gt; blue [5] =&gt; blue [6] =&gt; blue ) Array ()
Array ( [3] =&gt; blue [4] =&gt; blue [5] =&gt; blue [6] =&gt; blue ) Array ( [0] =&gt; red )
43. What will be the output of the following PHP code? <?php $a1 = array("red", "green"); $a2 = array("blue", "yellow"); print_r(array_merge($a1, $a2)); ?>
Array ( [0] =&gt; red [1] =&gt; green)
Array ( [0] =&gt; blue [1] =&gt; yellow [2] =&gt; red [3] =&gt; green )
Array ( [0] =&gt; red [1] =&gt; green [2] =&gt; blue [3] =&gt; yellow )
Array ( [0] =&gt; blue [1] =&gt; yellow )
44. What will be the output of the following PHP code? <?php $a = array("a"=>"red", "b"=>"green", "c"=>"blue"); echo array_shift($a); print_r ($a); ?>
green
red
redArray( [c] =&gt; green [c] =&gt; blue )
redArray( [b] =&gt; green [c] =&gt; blue )
45. What will be the output of the following PHP code? <?php $a = array("red", "green", "blue"); array_pop($a); print_r($a); ?>
Array ( [0] =&gt; red [1] =&gt; green )
Array ( [0] =&gt; green [1] =&gt; blue )
Array ( [0] =&gt; red [1] =&gt; blue )
Array ( [0] =&gt; blue [1] =&gt; blue )
46. What will be the output of the following PHP code? <?php $fruits = array ("mango", "apple", "pear", "peach"); $fruits = array_flip($fruits); echo ($fruits[0]); ?>
mango
error
peach
0
47. Which of the functions is used to sort an array in descending order?
sort()
asort()
rsort()
dsort()
48. What will be the output of the following PHP code? <?php $arr = array ("picture1.JPG", "picture2.jpg", "Picture10.jpg", "picture20.jpg"); sort($arr); print_r($arr); ?>
Array ( [0] =&gt; picture1.JPG [1] =&gt; Picture10.jpg [2] =&gt; picture2.jpg [3] =&gt; picture20.jpg )
Array ( [0] =&gt; picture1.JPG [1] =&gt; picture2.jpg [2] =&gt; Picture10.jpg [3] =&gt; picture20.jpg )
Array ( [0] =&gt; Picture10.jpg [1] =&gt; picture1.JPG [2] =&gt; picture2.jpg [3] =&gt; picture20.jpg )
Array ( [0] =&gt; Picture10.jpg [1] =&gt; picture1.JPG [2] =&gt; picture20.jpg [3] =&gt; picture2.jpg )
49. Which function should we use to sort the array in natural order?
dsort()
casesort()
natcasesort()
naturalsort()
50. What will be the output of the following PHP code? <?php $face = array ("A", "J", "Q", "K"); $number = array ("2","3","4", "5", "6", "7", "8", "9", "10"); $cards = array_merge ($face, $number); print_r ($cards); ?>
Array ( [0] =&gt; A [1] =&gt; J [2] =&gt; Q [3] =&gt; K [4] =&gt; 2 [5] =&gt; 3 [6] =&gt; 4 [7] =&gt; 5 [8] =&gt; 6 [9] =&gt; 7 [10] =&gt; 8 [11] =&gt; 9 [12] =&gt; 10 )
Array ( [0] =&gt; A [1] =&gt; 2 [2] =&gt; J [3] =&gt; 3 [4] =&gt; Q [5] =&gt; 4 [6] =&gt; K [7] =&gt; 5 [8] =&gt; 6 [9] =&gt; 7 [10] =&gt; 8 [11] =&gt; 9 [12] =&gt; 10 )
Error
Array ( [0] =&gt; 2 [1] =&gt; 3 [2] =&gt; 4 [3] =&gt; 5 [4] =&gt; 6 [5] =&gt; 7 [6] =&gt; 8 [7] =&gt; 9 [8] =&gt; 10 [9] =&gt; A [10] =&gt; J [11] =&gt; Q [12] =&gt; K )
51. What will be the output of the following PHP code? <?php $fruits = array ("apple", "mango", "peach", "pear", "orange"); $subset = array_slice ($fruits, 2); print_r ($subset); ?>
Array ( [0] =&gt; peach )
Array ( [0] =&gt; apple [1] =&gt; mango [2] =&gt; peach )
Array ( [0] =&gt; apple [1] =&gt; mango )
Array ( [0] =&gt; peach [1] =&gt; pear [2] =&gt; orange )
52. What will be the output of the following PHP code? <?php $fruits = array ("apple", "mango", "peach", "pear", "orange"); $subset = array_splice ($fruits, 2); print_r ($fruits); ?>
Error
Array ( [0] =&gt; apple [1] =&gt; mango [2] =&gt; peach )
Array ( [0] =&gt; apple [1] =&gt; mango ) d)
Array ( [0] =&gt; pear [1] =&gt; orange
53. What will be the output of the following PHP code? <?php $number = array ("4", "hello", 2); echo (array_sum ($number)); ?>
4hello2
4
2
6
54. What will be the output of the following PHP code? <?php $array1 = array ("KA", "LA", "CA", "MA", "TA"); $array2 = array ("KA", "IA", "CA", "GA", "TA"); $inter = array_intersect ($array1, $array2); print_r ($inter); ?>
Array ( [0] =&gt; KA [1] =&gt; LA [2] =&gt; CA [3] =&gt; MA [4] =&gt; TA [5] =&gt; IA [6] =&gt; GA )
Array ( [0] =&gt; KA [2] =&gt; CA [4] =&gt; TA )
Array ( [1] =&gt; IA [3] =&gt; GA )
Array ( [1] =&gt; LA [3] =&gt; MA )
Submit