MCQ

PHP Questions and Answers – Arrays – Part 1

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

1. How many types of array are accessible in PHP?

A 1

B 2

C 3

D 4

C
There are essentially three types of arrays in PHP: Indexed Arrays, Numeric Arrays, Associative Arrays and, Multidimensional Arrays.

 

 

2. The array index starts from ______?

A -1

B 0

C 1

D 2

B
By default, the starting index is zero.

 

 

3. Which the following declarations are correct to create an array in PHP?

A name[0] = "Alex";

B $name[] = array("Alex");

C $name[0] = "Alex";

D $name = array("Alex");

C, D
A variable name should start with $ which’s doesn’t exist in A and you don’t need to put the square brackets when you use the array() constructor.

 

 

4. ______ is an array with more than two dimensions.

A Multi dimensional array

B Single dimensional array

C Both A and B

D None of the above

A
Multidimensional array is an array with more than two dimensions.

 

 

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

A sort()

B asort()

C rsort()

D dsort()

C
sort() function is used to sort in ascending order while rsort() or reverse sort is used to sort an array in descending order.
 
Example :

<?php
  $names = array("Alex", "Bob", "Emily");
  rsort($names); // sort an array in descending order
?>

Output :

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

 

 

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

 

 

7. Which of the following built-in functions returns an array consisting of associative key/value pairs?

A count_values()

B array_count_values()

C array_count()

D count()

B
array_count_values() function count all the values of an array.
 
Example:

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

Output:

Array
(
    [A] => 1
    [B] => 1
    [C] => 1
)

 

 

8. What is the result of the following PHP code?
<?php

    $colors = array("Blue", "Orange", "Black");

    echo "I like " . $colors[2] . ", " . $colors[1] . " and " . $colors[0] . ".";

?>

A I like Black, Orange and Blue.

B I like Blue, Orange and Black.

C I like Orange, Blue and Black.

D None of the above

A
In the echo statement when we call the elements of array using its index, it will be displayed accordingly. As index ‘0’ indicates ‘Blue’, ‘1’ for ‘Orange’ and ‘2’ for Black’.
How to echo an array in PHPHow to echo an array in PHP?In this tutorial, we are going to see how to echo an array in PHP. Versus to “normal” variables, the values associated with array keys…Read More
 

9. What is the result of the following PHP code?
<?php

    $a = array("A","B","C","D","A");

    $b = array("A","E","F","D","A");

    $c = array_combine($a, $b);

    print_r($c);

?>

A Array([A] => A, [B] => E, [C] => F, [D] => D, [A] => A)

B Array([A] => A, [B] => E, [C] => F, [D] => D)

C Array([B] => E, [C] => F)

D None of the above

B
array_combine() function create an array using the elements from one “keys” array and one “values” array.

 

 

10. What is the result of the following PHP code?
<?php
    $arr = array ("stack", "how", "to", ".Com");
    echo(array_search(".com", $arr) );
?>

A No Output

B 0

C 3

D Error

A
Because of the case-sensitive .com is not in the array so there is no output.

 

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 *