1. Question: Array values are keyed by ______ values (called indexed arrays) or using ______ values (called associative arrays). Of course, these key methods can be combined as well.

    A
    Float, string

    B
    Positive number, negative number

    C
    Even number, string

    D
    String, Boolean

    E
    Integer, string

    Note: Arrays that are keyed by integer values are called indexed arrays, while those keyed by strings are called associative arrays. The correct answer is, therefore, E.
    1. Report
  2. Question: Consider the following array, called $multi_array. How would the value cat be referenced within the $multi_array array?
    <?php
    $multi_array = array("red",
                                     "green",
                                      42 => "blue",
                                      "yellow" => array("apple",  
                                                                   9 => "pear",  
                                                                   "banana",  
                                                                   "orange" => array("dog", "cat", "iguana")
                                                         )
                                       );
    ?>

    A
    $multi_array['yellow']['apple'][0]

    B
    $multi_array['blue'][0]['orange'][1]

    C
    $multi_array[3][3][2]

    D
    $multi_array['yellow']['orange']['cat']

    E
    $multi_array['yellow']['orange'][1]

    Note: The value cat is in an array buried within two other arrays. Following the path to the string, we see that, first, the yellow key must be referenced, followed by orange. Since the final array is an indexed array, the string cat is the second value and, therefore, has an index key of 1. Therefore, the correct answer is E.
    1. Report
  3. Question: What will the $array array contain at the end of the execution of the following script?
    <?php
    $array = array ('1', '1');
    foreach ($array as $k => $v) {
      $v = 2;
    }
    ?>

    A
    array ('2', '2')

    B
    array ('1', '1')

    C
    array (2, 2)

    D
    array (Null, Null)

    E
    array (1, 1)

    Note: Answer B is correct. The foreach construct operates on a copy of $array and, therefore, no changes are made to its original values.
    1. Report
  4. Question: Assume you would like to sort an array in ascending order by value while preserving key associations. Which of the following PHP sorting functions would you use?

    A
    ksort()

    B
    asort()

    C
    krsort()

    D
    sort()

    E
    usort()

    Note: Only the asort function sorts an array by value without destroying index associations. Therefore, Answer B is correct.
    1. Report
  5. Question: What are the name of functions used to convert an array into a string? Your Answer: ______ , ______

    A
    join()

    B
    explode()

    C
    join_str()

    D
    serialize()

    E
    join_string()

    Note: Not available
    1. Report
  6. Question: In what order will the following script output the contents of the $array array?
    <?php
    $array = array ('a1', 'a3', 'a5', 'a10', 'a20');
    natsort ($array);
    var_dump ($array);
    ?>

    A
    a1, a3, a5, a10, a20

    B
    a1, a20, a3, a5, a10

    C
    a10, a1, a20, a3, a5

    D
    a1, a10, a5, a20, a3

    E
    a1, a10, a20, a3, a5

    Note: The natsort() function uses a “natural ordering” algorithm to sort the contents of an array, rather than a simple binary comparison between the contents of each element. In fact, in this example the array is not even touched, since its elements are already in what could be considered a “natural” order. Therefore, Answer A is correct.
    1. Report
  7. Question: Which function would you use to rearrange the contents of the following array so that they are reversed (i.e.: array ('d', 'c', 'b', 'a') as the final result)? (Choose 2)
    <?php
     $array = array ('a', 'b', 'c', 'd');
    ?>

    A
    array_flip()

    B
    array_reverse()

    C
    sort()

    D
    rsort()

    E
    None

    Note: Despite its name, array_flip() only swaps each element of an array with its key. Both rsort() and array_reverse() would have the effect of reordering the array so that it contents would read ('d', 'c', 'b', 'a'). Therefore, the correct answers are B and D.
    1. Report
  8. Question: What will the following script output?
    <?php
    $array = array ('3' => 'a', '1b' => 'b', 'c', 'd');
    echo ($array[1]);
    ?>

    A
    1

    B
    b

    C
    c

    D
    A warning

    E
    a

    Note: PHP starts assigning numeric keys to elements without a hard-coded key from the lowest numeric key available (even if that key is a numeric string). If you never specify a numeric key to start with, it starts from zero. In our script, however, we assigned the key '3' to the very first element, thus causing the interpreter to assign the key 4 to the third element and 5 to the last element. Note that the key '1b' is not considered numeric, because it doesn’t evaluate to an integer number. Therefore, element 1 doesn’t exist, and Answer D is correct.
    1. Report
  9. Question: What is the simplest method of computing the sum of all the elements of an array?

    A
    By traversing the array with a for loop

    B
    By traversing the array with a foreach loop

    C
    By using the array_intersect function

    D
    By using the array_sum function

    E
    By using array_count_values()

    Note: The array_sum function calculates the sum of all the elements of an array. Therefore, Answer D is correct.
    1. Report
  10. Question: What will the following script output?
    <?php
    $array = array (0.1 => 'a', 0.2 => 'b');
    echo count ($array);
    ?>

    A
    1

    B
    2

    C
    0

    D
    Nothing

    E
    0.3

    Note: The script will output 1 (Answer A). This is because only integer numbers and strings can be used as keys of an array—floating-point numbers are converted to integers. In this case, both 0.1 and 0.2 are converted to the integer number 0, and $array will only contain the element 0 => 'b'.
    1. Report
Copyright © 2024. Powered by Intellect Software Ltd