MCQ

PHP Questions and Answers – Arrays – Part 4

This collection of PHP Multiple Choice Questions and Answers (MCQs): Quizzes & Practice Tests with Answer focuses on “PHP Arrays”.
 

1. What is the result of the following PHP code?
<?php 
    $number = array("2", "str", 4);
    echo(array_sum($number));
?>

A 4

B 2

C 6

D 2str4

C
The built-in function array_sum() add all the values of the input array, returning the sum. If a string is found, it will be ignored.

 

 

2. What is the result of the following PHP code?
<?php 
    $arr = array ("A", "B", "C", "D", "E");
    $splice = array_splice ($arr, 2);
    print_r ($arr);
?>

A Array([0] => D, [1] => E)

B Array([0] => A, [1] => B)

C Array([3] => D, [4] => E)

D Array([0] => C, [1] => D)

B
The built-in function array_splice() removes all elements of an array found within a specified range.

 

 

3. What is the result of the following PHP code?
<?php
    $age = array("alex"=>"18", "bob"=>"25", "emily"=>"55");
    print_r(array_change_key_case($age, CASE_UPPER));
?>

A Array([Alex] => 18, [Bob] => 25, [Emily] => 55)

B Array([ALEX] => 18, [BOB] => 25, [EMILY] => 55)

C Array([alex] => 18, [bob] => 25, [emily] => 55)

D None of the above

B
The built-in function array_change_key_case() changes all keys in an array to lowercase or uppercase.

 

 

4. What is the result of the following PHP code?
<?php
    $names = array("Alex", "Bob", "Emily", "Ali");
    print_r(array_chunk($names, 2));
?>

A

Array
(
    [0] => Array
        (
            [0] => Alex
            [1] => Bob
        )
    [1] => Array
        (
            [0] => Emily
            [1] => Ali
        )
)

B

Array
(
    [1] => Array
        (
            [0] => Alex
            [1] => Bob
        )
    [2] => Array
        (
            [0] => Emily
            [1] => Ali
        )
)

C

Array
(
      [0] => Alex
      [1] => Bob
)
Array
(
      [0] => Emily
      [1] => Ali
)

D None of the above

A
The built-in function array_chunk() splits an array into chunks of new arrays.

 

 

5. Which of the following built-in functions is used to sort the array in the natural order?

A casesort()

B dsort()

C natcasesort()

D naturalsort()

C
The built-in function natcasesort() sorts an array by using a natural order algorithm. All the values maintain their original keys.

 

 

6. What is the result of the following PHP code?
<?php
    $names = array("Alex", "Bob", "Emily", "Ali");
    $res = array_slice ($names, 2);
    print_r ($res);
?>

A Array([0] => Bob [1] => Emily [2] => Ali)

B Array([0] => Ali [1] => Emily)

C Array([0] => Emily [1] => Ali)

D None of the above

C
The built-in function array_slice() returns a portion of an array based on a starting and ending offset value.

 

 

7. What is the result of the following PHP code?
<?php
    $arr = array("A"=>"Alex", "B"=>"Bob", "E"=>"Emily");
    echo array_shift($arr);
    print_r ($arr);
?>

A Array([B] => Bob [E] => Emily)

B Array([A] => Alex [B] => Bob [E] => Emily)

C AlexArray([B] => Bob [E] => Emily)

D None of the above

C
The built-in function array_shift() removes the first element from an array, and returns the value of the removed element.

 

 

8. What is the result of the following PHP code?
<?php
    $a = array("Alex", "Bob", "Emily");
    array_pop($a);
    print_r($a);
?>

A Array([0] => Alex [1] => Bob [2] => Emily)

B Array([0] => Bob [1] => Emily)

C Array([0] => Alex [1] => Bob)

D Array([1] => Alex [2] => Bob)

C
The built-in function array_pop() deletes the last element of an array.

 

 

9. What is the result of the following PHP code?
<?php 
    $animals = array ("Cat", "Dog", array ("Tiger", "Alligator"), "Mouse");
    echo (count($animals, 1));
?>

A 4

B 5

C 6

D 7

C
The array entity holding “Tiger” and “Alligator” is counted as an item, just as its contents are.

 

 

10. Which of the following built-in functions is used to add a value to the end of an array?

A add_array()

B array_unshift()

C inend_array()

D array_push()

D
array_push allow us to add a value to the end of an array, and it return the total of elements in the array after the new value has been added.
 
Example:

<?php
    $arr = array("A", "B");
    
    array_push($arr, "C", "D");
    
    print_r($arr);
?>

Output:

Array
(
    [0] => A
    [1] => B
    [2] => C
    [3] => D
)

 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

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