MCQ

PHP Questions and Answers – Arrays – Part 2

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

1. In comparison with associative arrays vector are often ________

A Faster

B Slower

C Stable

D None of the above

A
In comparison with associative arrays vector are often Faster.

 

 

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

A 1

B 2

C 3

D 4

B
The next function is used to display the next value of the current index. As the current index value is 0 so next function will display the value “2”.

 

 

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

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

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

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

D None of the above

B
The function array_merge() allow us to merge arrays into one array.

 

[st_adsense]  

4. Which of the following built-in functions return true if the parameter is an array or false if it is not an array?

A in_array()

B is_array()

C this_array()

D do_array()

B
is_array() function is used to check whether a variable is an array or not.
 
Example:

<?php
    $var1 = "Welcome!";

    if (is_array($var1)) 
        echo "$var1 is an array. \n"; 
    else
        echo "$var1 is not an array. \n"; 
 
    
    $var2 = array("A", "B", "C", "D");

    if (is_array($var2)) 
        echo "array('A', 'B', 'C', 'D') is an array. \n"; 
    else
        echo "array('A', 'B', 'C', 'D') is not an array. \n"; 
?>

Output:

Welcome! is not an array. 
array('A', 'B', 'C', 'D') is an array.

 

 

5. What is the result of the following PHP code?
<?php
    $colors = array ("Blue", "Black", "Yellow", "Green");
    $colors = array_flip($colors);
    echo ($colors[0]);
?>

A Blue

B Green

C 0

D Error

D
array_flip() function flip an array, i.e. turns keys into values and values into keys. As we are flipping the values, $fcolors[“Blue”] = 0, $colors[“Black”] = 1 and so on.

 

 

6. What is the result of the following PHP code?
<?php
    $languages = array ("PHP", "Java", "C++");
    echo (next($languages));	
    echo (next($languages));
?>

A PHPJava

B PHPC++

C JavaC++

D None of the above

C
The next function is used to display the next value of the current index.

 

 

7. Which of the following built-in functions allow us to get the previous element in an array?

A previous()

B prev()

C before()

D last()

B
The built-in function prev() returns the previous element in the array.

 

[st_adsense]  

8. What is the result of the following PHP code?
<?php
    $states = array(
                    "United states" => 
                        array(
                            "population" => "22,12,000", 
                            "captial" => "Washington"
                        ),
                        "Washington" => 
                            array( 
                                "population" => "9,80,000", 
                                "captial" => "Washington"
                            )
    );
    
    echo  $states["United states"]["population"];
?>

A 22,12,000

B 9,80,000

C United states population

D United states 22,12,000

A
states is a multidimensional array and we traverse it to get the value.

 

 

9. What is the result of the following PHP code?
<?php
    $array = array (1, 2, 3, 4, 1);
    $sum = 0;
    
    for ($i = 0; $i < 4; $i++) {
        $sum += $array[$array[$i]];
    }
    
    echo $sum;
?>

A 11

B 10

C 12

D 9

B
Here $array[$array[$i]]; we use array’s value as index, so the output is 10.

 

 

10. What is the result of the following PHP code?
<?php
    $array = array (true => 'a', 1 => 'b');
    var_dump ($array);
?>

A True => ‘a’, 1 => ‘b’

B 1 => ‘b’

C 0 => ‘a’, 1 => ‘b’

D None

B
The boolean value true is displayed as 1 so the key is duplicated. If we change the code as bellow:

<?php
    $array = array (false => 'a', 1 => 'b');
    var_dump ($array);
?>

It will work, because the boolean value false is displayed as 0 so there is no key duplication.

array(2) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
}

 

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

Leave a Reply

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