MCQ

PHP Questions and Answers – Arrays – Part 3

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

1. Which of the following built-in functions is used to check if the specified key exists in the array?

A arrays_key_exists()

B array_keys_exists()

C array_key_exists()

D array_key_exist()

C
The built-in function array_key_exists() is used to check if the specified key exists in the array.
 
Example:

<?php
    $arr = array("A" => "Alex", "B" => "Bob", "E" => "Emily");
    
    if (array_key_exists("B", $arr))
    {
      echo "Key exists!";
    }
    else
    {
      echo "Key does not exist!";
    }
?>

Output:

Key exists!

 

 

2. If we transfer arrays by value to a read-only function is it decreasing the performance of execution in comparison with transferring them by reference?

A Yes, since the interpreter needs constantly to create a copy of the array before transfer it to the function.

B Yes, but only if the function changes the array values.

C Yes, but only if the array is big.

D Yes, since PHP should monitor the execution of the function to determine if changes are made to the array.

E No.

E
No. Transfering arrays by value to a read-only function is not decreasing the performance of execution in comparison with transferring them by reference

 

 

3. Which of the following built-in functions is used to compute the difference of arrays?

A arrays_dif()

B diff_array()

C array_diff()

D dif_arrays()

C
The built-in function array_diff() is used to compare the values of two or more arrays, and returns the differences.
 
Example:

<?php
    $arr1 = array("A"=>"Alex", "B"=>"Bob", "E"=>"Emily");
    $arr2 = array("L"=>"Alex", "O"=>"Bob");
    
    $diff = array_diff($arr1, $arr2);
    print_r($diff);
?>

Output:

Array
(
    [E] => Emily
)

 

 

4. Suppose you want to sort an array in ascending order by value while keeping key associations. Which of the following built-in functions would you use?

A sort()

B usort()

C krsort()

D asort()

E ksort()

D
The built-in function asort() used to sort an array in ascending order by value while keeping key associations.
 
Example:

<?php
    $age = array("Alex"=>"50", "Bob"=>"28", "Emily"=>"18");
    asort($age);
?>

Output:

Array
(
    [Emily] => 18
    [Bob] => 28
    [Alex] => 50
)

 

 

5. Which of the following built-in functions is used to count elements in an array?

A Count_array

B Array_Count

C Sizeof

D count

C, D
Sizeof and count both are used to count elements in an array.
 
Example:

<?php
    $names = array("Alex", "Bob", "Emily");
    echo sizeof($names);
    echo "\n";
    echo count($names);
?>

Output:

3
3

 

 

6. What is the result of the following PHP code?
<?php
    $arr1 = array("A"=>"Alex", "B"=>"Bob", "E"=>"Emily");
    $arr2 = array("L"=>"Alex", "O"=>"Bob");
    
    $result = array_intersect($arr1, $arr2);
    print_r($result);
?>

A Array([A] => Alex, [B] => Bob)

B Array([L] => Alex, [O] => Bob)

C Array([E] => Emily)

D Array()

A
The built-in function array_intersect() compares the values of two or more arrays, and returns the matches.

 

 

7. What is the result of the following PHP code?
<?php
    $arr = array(2, 1, 3);
    echo(array_product($arr));
?>

A 3

B 2

C 6

D 1

C
The built-in function array_product() calculates and returns the product of an array.

 

 

8. What is the result of the following PHP code?
<?php
    $arr1 = array("A", "B");
    $arr2 = array("C", "D");
    $arr3 = array_replace($arr1, $arr2);
    print_r($arr3);
?>

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

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

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

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

A
The built-in function array_replace() replaces the values of the 1st array with the values from 2nd arrays.

 

 

9. What is the result of the following PHP code?
<?php
    $arr = array("A", "B", "C", "D");
    echo pos($arr);
?>

A D

B A

C 0

D True

B
The built-in function pos() returns the value of the current element in an array, and since no operation has been done, the current element is the first element.

 

 

10. What is the result of the following PHP code?
<?php
    $nbr = range(0, 2);
    print_r ($nbr);
?>

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

B Array([1] => 1, [2] => 2)

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

D None of the above

A
The built-in function range() creates an array containing a range of elements.

 

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 *