This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “Arrays – 5”.
1. What will be the output of the following PHP code?
<?php
$fruits = array ("mango", "apple", "pear", "peach");
$fruits = array_flip($fruits);
echo ($fruits[0]);
?>
a) mango
b) error
c) peach
d) 0
View Answer
Explanation: As we are flipping the values, $fruits[“mango”] = 0, $fruits[“apple”] = 1 and so on.
2. Which of the functions is used to sort an array in descending order?
a) sort()
b) asort()
c) rsort()
d) dsort()
View Answer
Explanation: The function sort() will sort the arrays in ascending order, the function rsort() will sort arrays in descending order. While the function asort() will sort associative arrays in ascending order, according to the value.
3. What will be the output of the following PHP code?
<?php
$fruits = array ("mango", "apple", "peach", "pear");
$fruits = asort ($fruits);
printr ($fruits);
?>
a) Array ( [1] => apple [0] => mango [2] => peach [3] => pear )
b) Array ( [0] => apple [1] => mango [2] => peach [3] => pear )
c) Error
d) Array ( [1] => apple [0] => mango [3] => peach [2] => pear )
View Answer
Explanation: The program will give an error i.e. uncaught error: call to undefined function printr(). As the correct function is print_r().
4. 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);
?>
a) Array ( [0] => picture1.JPG [1] => Picture10.jpg [2] => picture2.jpg [3] => picture20.jpg )
b) Array ( [0] => picture1.JPG [1] => picture2.jpg [2] => Picture10.jpg [3] => picture20.jpg )
c) Array ( [0] => Picture10.jpg [1] => picture1.JPG [2] => picture2.jpg [3] => picture20.jpg )
d) Array ( [0] => Picture10.jpg [1] => picture1.JPG [2] => picture20.jpg [3] => picture2.jpg )
View Answer
Explanation: The function sort() in PHP sorts an indexed array in ascending order. While sorting, each character is compared with the others and sorted using ASCII values.
5. Which function should we use to sort the array in natural order?
a) dsort()
b) casesort()
c) natcasesort()
d) naturalsort()
View Answer
Explanation: The function natcasesort() in PHP sorts an array by using a “natural order” algorithm. All the values keep their original keys. Eg: In a natural algorithm, as the number 2 is less than the number 10. But in computer sorting, 10 is less than 2, because the first number in “10” is less than 2. The function is case-insensitive.
6. 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);
?>
a) Array ( [0] => A [1] => J [2] => Q [3] => K [4] => 2 [5] => 3 [6] => 4 [7] => 5 [8] => 6 [9] => 7 [10] => 8 [11] => 9 [12] => 10 )
b) Array ( [0] => A [1] => 2 [2] => J [3] => 3 [4] => Q [5] => 4 [6] => K [7] => 5 [8] => 6 [9] => 7 [10] => 8 [11] => 9 [12] => 10 )
c) Error
d) Array ( [0] => 2 [1] => 3 [2] => 4 [3] => 5 [4] => 6 [5] => 7 [6] => 8 [7] => 9 [8] => 10 [9] => A [10] => J [11] => Q [12] => K )
View Answer
Explanation: The array_merge() function merges one or more arrays into one array. The resulting array will begin with the first input array parameter, appending each subsequent array parameter in the order of appearance.
7. 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);
?>
a) Array ( [0] => peach )
b) Array ( [0] => apple [1] => mango [2] => peach )
c) Array ( [0] => apple [1] => mango )
d) Array ( [0] => peach [1] => pear [2] => orange )
View Answer
Explanation: The array_slice() function returns a section of an array based on a starting and ending offset value.
8. 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);
?>
a) Error
b) Array ( [0] => apple [1] => mango [2] => peach )
c) Array ( [0] => apple [1] => mango )
d) Array ( [0] => pear [1] => orange )
View Answer
Explanation: The array_splice() function removes all elements of an array found within a specified range.
9. What will be the output of the following PHP code?
<?php
$number = array ("4", "hello", 2);
echo (array_sum ($number));
?>
a) 4hello2
b) 4
c) 2
d) 6
View Answer
Explanation: The array_sum() function add all the values of the input array together, returning the final sum. If a string datatype is found, it’ll be ignored.
10. 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);
?>
a) Array ( [0] => KA [1] => LA [2] => CA [3] => MA [4] => TA [5] => IA [6] => GA )
b) Array ( [0] => KA [2] => CA [4] => TA )
c) Array ( [1] => IA [3] => GA )
d) Array ( [1] => LA [3] => MA )
View Answer
Explanation: The array_intersect() function returns a key preserved array consisting only of those values present in the first array that are also present in each of the other input arrays.
Sanfoundry Global Education & Learning Series – PHP Programming.
To practice all questions on PHP Programming, here is complete set of 1000+ Multiple Choice Questions and Answers on PHP.
- Practice Programming MCQs
- Apply for Programming Internship
- Practice MCA MCQs
- Check Information Technology Books
- Check PHP Books