250+ TOP PHP Coding MCQs on Arrays and Answers

PHP Multiple Choice Questions on “Arrays – 4”.

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

  1. $cars = array("Volvo", "BMW", "Toyota");
  2. echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
  3. ?>

a) I like Volvo BMW and Toyota.
b) I like Volvo, BMW and Toyota)
c) I like Volvo, BMW and Toyota.
d) I like. Volvo.,. BMW. and. Toyota)
Answer: b
Clarification: The array() function is used to create an array. Each elements are assigned ab index as the rule of an array. So, calling $cars[0] will print element at index 0 and so on.

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

  1. $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
  2. print_r(array_change_key_case($age, CASE_UPPER));
  3. ?>

a) Array ( [Peter] => 35 [Ben] => 37 [Joe] => 43 )
b) Array ( [peter] => 35 [ben] => 37 [joe] => 43 )
c) Array ( [PETER] => 35 [BEN] => 37 [JOE] => 43 )
d) Array ( [PeTeR] => 35 [BeN] => 37 [Joe] => 43 )
Answer: c
Clarification: The array_change_key_case() function changes all keys in an array to lowercase or uppercase.

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

  1. $cars = array("Volvo", "BMW", "Toyota", "Honda", "Mercedes", "Opel");
  2. print_r(array_chunk($cars, 2));
  3. ?>

a) Array ( [0] => Array ( [1] => Volvo [2] => BMW ) [1] => Array ( [1] => Toyota [2] => Honda ) [2] => Array ( [1] => Mercedes [2] => Opel ) )
b) Array ( [1] => Array ( [1] => Volvo [2] => BMW ) [2] => Array ( [1] => Toyota [2] => Honda ) [3] => Array ( [1] => Mercedes [2] => Opel ) )
c) Array ( [0] => Array ( [0] => Volvo [1] => Volvo ) [1] => Array ( [0] => BMW [1] => BMW ) [2] => Array ( [0] => Toyota [1] => Toyota ) )
d) Array ( [0] => Array ( [0] => Volvo [1] => BMW ) [1] => Array ( [0] => Toyota [1] => Honda ) [2] => Array ( [0] => Mercedes [1] => Opel ) )

Answer: d
Clarification: The array_chunk() function splits an array into chunks of new arrays.

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

  1. $fname = array("Peter", "Ben", "Joe");
  2. $age = array("35", "37", "43");
  3. $c = array_combine($fname, $age);
  4. print_r($c);
  5. ?>

a) Array ( Peter Ben Joe )
b) Array ( [Peter] => 35 [Ben] => 37 [Joe] => 43 )
c) Array ( 35 37 43 )
d) Array ( “[Peter] => 35” “[Ben] => 37” “[Joe] => 43” )
Answer: b
Clarification: The array_combine() function creates an array by using the elements from one “keys” array and one “values” array.

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

  1. $a = array("A", "Cat", "Dog", "A", "Dog");
  2. print_r(array_count_values($a));
  3. ?>

a) Array ( [A] => 2 [Cat] => 1 [Dog] => 2 )
b) Array ( [A] => 2 [Cat] => 2 [Dog] => 1 )
c) Array ( [A] => 1 [Cat] => 1 [Dog] => 2 )
d) Array ( [A] => 2 [Cat] => 1 [Dog] => 1)
Answer: a
Clarification: The array_count_values() function counts all the values of an array.

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

  1. $a1 = array("a"=>"red", "b"=>"green", "c"=>"blue", "d"=>"yellow");
  2. $a2 = array("e"=>"red", "f"=>"green", "g"=>"blue");
  3. $result = array_diff($a1, $a2);
  4. print_r($result);
  5. ?>

a) Array ( [d] => yellow )
b) Array ( [c] => blue )
c) Array ( [a] => red )
d) Array ( [e] => yellow )
Answer: a
Clarification: The array_diff() function compares the values of two (or more) arrays, and returns the differences.

  250+ TOP MCQs on PHP Updating and Deleting Entries and Answers

7. What will be the output of the following PHP code?

  1. $a1 = array_fill(3, 4, "blue");
  2. $b1 = array_fill(0, 1, "red");
  3. print_r($a1);
  4. echo "
    "
    ;
  5. print_r($b1);
  6. ?>

a)

  Array ( [3] => blue [4] => blue) 
  Array ( [0] => red )

b)

  Array ( [4] => blue [5] => blue [6] => blue) 
  Array ( [0] => red )

c)

  Array ( [3] => blue [4] => blue [5] => blue [6] => blue ) 
  Array ()

d)

  Array ( [3] => blue [4] => blue [5] => blue [6] => blue ) 
  Array ( [0] => red )

Answer: d
Clarification: The array_fill() function fills an array with values.

 
 

8. What will be the output of the following PHP code?

  1. $a1 = array("red", "green");
  2. $a2 = array("blue", "yellow");
  3. print_r(array_merge($a1, $a2));
  4. ?>

a) Array ( [0] => red [1] => green)
b) Array ( [0] => blue [1] => yellow [2] => red [3] => green )
c) Array ( [0] => red [1] => green [2] => blue [3] => yellow )
d) Array ( [0] => blue [1] => yellow )
Answer: c
Clarification: The array_merge() function merges one or more arrays into one array.

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

  1. $a = array("a"=>"red", "b"=>"green", "c"=>"blue");
  2. echo array_shift($a);
  3. print_r ($a);
  4. ?>

a) green
b) red
c) redArray( [c] => green [c] => blue )
d) redArray( [b] => green [c] => blue )
Answer: d
Clarification: The array_shift() function removes the first element from an array, and returns the value of the removed element.

  250+ TOP PHP Coding MCQs on Operators and Answers

10. What will be the output of the following PHP code?

  1. $a = array("red", "green", "blue");
  2. array_pop($a);
  3. print_r($a);
  4. ?>

a) Array ( [0] => red [1] => green )
b) Array ( [0] => green [1] => blue )
c) Array ( [0] => red [1] => blue )
d) Array ( [0] => blue [1] => blue )
Answer: a
Clarification: The array_pop() function deletes the last element of an array.

Leave a Comment

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

Scroll to Top