PHP Quizs Question & Answers Arrays – 5

Q 1. What will be the output of the following PHP code?

  1. <?php
  2. $arr = array (“picture1.JPG”, “picture2.jpg”,
  3. “Picture10.jpg”, “picture20.jpg”);
  4. sort($arr);
  5. print_r($arr);
  6. ?>

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 )

Show Answer Answer:-C. Array ( [0] => Picture10.jpg [1] => picture1.JPG [2] => picture2.jpg [3] => picture20.jpg )
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.

Q 2. What will be the output of the following PHP code?

  1. <?php
  2. $face = array (“A”, “J”, “Q”, “K”);
  3. $number = array (“2″,”3″,”4”, “5”, “6”, “7”, “8”, “9”, “10”);
  4. $cards = array_merge ($face, $number);
  5. print_r ($cards);
  6. ?>

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 )

Show Answer Answer:-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 )
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.

Q 3. What will be the output of the following PHP code?

  1. <?php
  2. $fruits = array (“apple”, “mango”, “peach”, “pear”,
  3. “orange”);
  4. $subset = array_splice ($fruits, 2);
  5. print_r ($fruits);
  6. ?>

A. Error
B. Array ( [0] => apple [1] => mango [2] => peach )
C. Array ( [0] => apple [1] => mango )
D. Array ( [0] => pear [1] => orange )

Show Answer Answer:-C. Array ( [0] => apple [1] => mango )
Explanation The array_splice() function removes all elements of an array found within a specified range.

Q 4. What will be the output of the following PHP code?

  1. <?php
  2. $array1 = array (“KA”, “LA”, “CA”, “MA”, “TA”);
  3. $array2 = array (“KA”, “IA”, “CA”, “GA”, “TA”);
  4. $inter = array_intersect ($array1, $array2);
  5. print_r ($inter);
  6. ?>

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 )

Show Answer Answer:-B. Array ( [0] => KA [2] => CA [4] => TA )
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

Q 5. What will be the output of the following PHP code?

  1. <?php
  2. $fruits = array (“mango”, “apple”, “pear”, “peach”);
  3. $fruits = array_flip($fruits);
  4. echo ($fruits[0]);
  5. ?>

A. mango
B. error
C. peach
D. 0

Show Answer Answer:-B. error
Explanation As we are flipping the values, $fruits[“mango”] = 0, $fruits[“apple”] = 1 and so on.

Q 6. What will be the output of the following PHP code?

  1. <?php
  2. $fruits = array (“mango”, “apple”, “peach”, “pear”);
  3. $fruits = asort ($fruits);
  4. printr ($fruits);
  5. ?>

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 )

Show Answer Answer:-C. Error
Explanation The program will give an error i.e. uncaught error: call to undefined function printr(). As the correct function is print_r().

Q 7. Which function should we use to sort the array in natural order?
A. dsort()
B. casesort()
C. natcasesort()
D. naturalsort()

Show Answer Answer:-C. natcasesort()
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.

Q 8. Which of the functions is used to sort an array in descending order?
A. sort()
B. asort()
C. rsort()
D. dsort()

Show Answer Answer:-C. rsort()
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.

Q 9. What will be the output of the following PHP code?

  1. <?php
  2. $fruits = array (“apple”, “mango”, “peach”, “pear”,
  3. “orange”);
  4. $subset = array_slice ($fruits, 2);
  5. print_r ($subset);
  6. ?>

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 )

Show Answer Answer:-D. Array ( [0] => peach [1] => pear [2] => orange )
Explanation The array_slice() function returns a section of an array based on a starting and ending offset value.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You cannot copy content of this page