PHP Coding Questions and Answers – Arrays – 4

PHP Multiple Choice Questions & Answers (MCQs) focuses on “Arrays – 4”.

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


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 2. What will be the output of the following PHP code?

“35”, “Ben”=>”37”, “Joe”=>”43”);
print_r(array_change_key_case($age, CASE_UPPER));
?>
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:-B. 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 3. What will be the output of the following PHP code?


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 4.What will be the output of the following PHP code?


A. Array ( Peter Ben Joe )
B. Array ( 35 37 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_combine() function creates an array by using the elements from one “keys” array and one “values” array.

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

“red”, “b”=>”green”, “c”=>”blue”, “d”=>”yellow”);
$a2 = array(“e”=>”red”, “f”=>”green”, “g”=>”blue”);
$result = array_diff($a1, $a2);
print_r($result);
?>
A. Array ( [c] => blue )
B. Array ( [d] => yellow )
C. Array ( [a] => red )
D. Array ( [e] => yellow )

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

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

“;
print_r($b1);
?>
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 7. What will be the output of the following PHP code?

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

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

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

“red”, “b”=>”green”, “c”=>”blue”);
echo array_shift($a);
print_r ($a);
?>
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 9. What will be the output of the following PHP code?


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

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


Posted

in

,

by

Comments

Leave a Reply

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

You cannot copy content of this page