PHP Coding Quizs Question & Answers Arrays – 4

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

  1. <?php
  2. $cars = array(“Volvo”, “BMW”, “Toyota”, “Honda”, “Mercedes”, “Opel”);
  3. print_r(array_chunk($cars, 2));
  4. ?>

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 ) )

Show Answer Answer:-D. Array ( [0] => Array ( [0] => Volvo [1] => BMW ) [1] => Array ( [0] => Toyota [1] => Honda ) [2] => Array ( [0] => Mercedes [1] => Opel ) )
Explanation The array_chunk() function splits an array into chunks of new arrays.

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

  1. <?php
  2. $a = array(“A”, “Cat”, “Dog”, “A”, “Dog”);
  3. print_r(array_count_values($a));
  4. ?>

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)

Show Answer Answer:-A. Array ( [A] => 2 [Cat] => 1 [Dog] => 2 )
Explanation The array_count_values() function counts all the values of an array.

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

  1. <?php
  2. $a1 = array_fill(3, 4, “blue”);
  3. $b1 = array_fill(0, 1, “red”);
  4. print_r($a1);
  5. echo “<br>”;
  6. print_r($b1);
  7. ?>

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 )

Show Answer Answer:-D. Array ( [3] => blue [4] => blue [5] => blue [6] => blue ) Array ( [0] => red )
Explanation The array_fill() function fills an array with values.

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

  1. <?php
  2. $age = array(“Peter”=>”35”, “Ben”=>”37”, “Joe”=>”43”);
  3. print_r(array_change_key_case($age, CASE_UPPER));
  4. ?>

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 )

Show Answer Answer:-C. Array ( [PETER] => 35 [BEN] => 37 [JOE] => 43 )
Explanation The array_change_key_case() function changes all keys in an array to lowercase or uppercase.

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

  1. <?php
  2. $a1 = array(“red”, “green”);
  3. $a2 = array(“blue”, “yellow”);
  4. print_r(array_merge($a1, $a2));
  5. ?>

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 )

Show Answer Answer:-C. Array ( [0] => red [1] => green [2] => blue [3] => yellow )
Explanation The array_merge() function merges one or more arrays into one array.

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

  1. <?php
  2. $a = array(“a”=>”red”, “b”=>”green”, “c”=>”blue”);
  3. echo array_shift($a);
  4. print_r ($a);
  5. ?>

A. green
B. red
C. redArray( [c] => green [c] => blue )
D. redArray( [b] => green [c] => blue )

Show Answer Answer:-D. redArray( [b] => green [c] => blue )
Explanation The array_shift() function removes the first element from an array, and returns the value of the removed element.

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

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

A. Array ( [d] => yellow )
B. Array ( [c] => blue )
C. Array ( [a] => red )
D. Array ( [e] => yellow )

Show Answer Answer:-A. Array ( [d] => yellow )
Explanation The array_diff() function compares the values of two (or more) arrays, and returns the differences.

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

  1. <?php
  2. $a = array(“red”, “green”, “blue”);
  3. array_pop($a);
  4. print_r($a);
  5. ?>

A. Array ( [0] => red [1] => green )
B. Array ( [0] => green [1] => blue )
C. Array ( [0] => red [1] => blue )
D. Array ( [0] => blue [1] => blue )

Show Answer Answer:-A. Array ( [0] => red [1] => green )
Explanation The array_pop() function deletes the last element of an array.

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

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

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)

Show Answer Answer:-C. I like Volvo, BMW and Toyota.
Explanation 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.

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

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

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” )

Show Answer Answer:-B. Array ( [Peter] => 35 [Ben] => 37 [Joe] => 43 )
Explanation The array_combine() function creates an array by using the elements from one “keys” array and one “values” array.

Comments

Leave a Reply

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

You cannot copy content of this page