PHP Questions and Answers – Arrays – Part 4
This collection of PHP Multiple Choice Questions and Answers (MCQs): Quizzes & Practice Tests with Answer focuses on “PHP Arrays”.
1. What is the result of the following PHP code?
<?php $number = array("2", "str", 4); echo(array_sum($number)); ?>
A 4
B 2
C 6
D 2str4
2. What is the result of the following PHP code?
<?php $arr = array ("A", "B", "C", "D", "E"); $splice = array_splice ($arr, 2); print_r ($arr); ?>
A Array([0] => D, [1] => E)
B Array([0] => A, [1] => B)
C Array([3] => D, [4] => E)
D Array([0] => C, [1] => D)
3. What is the result of the following PHP code?
<?php $age = array("alex"=>"18", "bob"=>"25", "emily"=>"55"); print_r(array_change_key_case($age, CASE_UPPER)); ?>
A Array([Alex] => 18, [Bob] => 25, [Emily] => 55)
B Array([ALEX] => 18, [BOB] => 25, [EMILY] => 55)
C Array([alex] => 18, [bob] => 25, [emily] => 55)
D None of the above
4. What is the result of the following PHP code?
<?php $names = array("Alex", "Bob", "Emily", "Ali"); print_r(array_chunk($names, 2)); ?>
A
Array ( [0] => Array ( [0] => Alex [1] => Bob ) [1] => Array ( [0] => Emily [1] => Ali ) )
B
Array ( [1] => Array ( [0] => Alex [1] => Bob ) [2] => Array ( [0] => Emily [1] => Ali ) )
C
Array ( [0] => Alex [1] => Bob ) Array ( [0] => Emily [1] => Ali )
D None of the above
5. Which of the following built-in functions is used to sort the array in the natural order?
A casesort()
B dsort()
C natcasesort()
D naturalsort()
6. What is the result of the following PHP code?
<?php $names = array("Alex", "Bob", "Emily", "Ali"); $res = array_slice ($names, 2); print_r ($res); ?>
A Array([0] => Bob [1] => Emily [2] => Ali)
B Array([0] => Ali [1] => Emily)
C Array([0] => Emily [1] => Ali)
D None of the above
7. What is the result of the following PHP code?
<?php $arr = array("A"=>"Alex", "B"=>"Bob", "E"=>"Emily"); echo array_shift($arr); print_r ($arr); ?>
A Array([B] => Bob [E] => Emily)
B Array([A] => Alex [B] => Bob [E] => Emily)
C AlexArray([B] => Bob [E] => Emily)
D None of the above
8. What is the result of the following PHP code?
<?php $a = array("Alex", "Bob", "Emily"); array_pop($a); print_r($a); ?>
A Array([0] => Alex [1] => Bob [2] => Emily)
B Array([0] => Bob [1] => Emily)
C Array([0] => Alex [1] => Bob)
D Array([1] => Alex [2] => Bob)
9. What is the result of the following PHP code?
<?php $animals = array ("Cat", "Dog", array ("Tiger", "Alligator"), "Mouse"); echo (count($animals, 1)); ?>
A 4
B 5
C 6
D 7
10. Which of the following built-in functions is used to add a value to the end of an array?
A add_array()
B array_unshift()
C inend_array()
D array_push()