1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. Question: What elements will the following script output?
    <?php
    $array = array (true => 'a', 1 => 'b');
    var_dump ($aray);
    ?>

    A
    1 => 'b'

    B
    True => 'a', 1 => 'b'

    C
    0 => 'a', 1 => 'b'

    D
    None

    E
    It will output NULL

    Note: This question tries to attract your attention to a problem that doesn’t bear on its answer. The $array array will contain only one element, since true evaluates to the integer 1. However, there is a typo in the var_dump() statement—$array is misspelled as $aray, using only one ‘r’. Therefore, the var_dump() statement will output NULL (and, possibly, a notice, depending on your error settings). Answer E is correct.
    1. Report
  9. Question: Absent any actual need for choosing one method over the other, does passing arrays by value to a read-only function reduce performance compared to passing them by reference?

    A
    Yes, because the interpreter must always create a copy of the array before passing it to the function.

    B
    Yes, but only if the function modifies the contents of the array.

    C
    Yes, but only if the array is large.

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

    E
    No.

    Note: This question is a bit convoluted, so it’s easy to get lost in it. For starters, notice that it specifies two important assumptions: first, that you do not have any compelling reason for passing the array either way. If you needed a function to modify the array’s contents, you’d have no choice but to pass it by reference—but that’s not the case here. Second, the question specifies that we’re passing the array to a read-only function; if this were not the case, Answer B would be true, since a change of the array would cause an actual copy of the array to be created. As a general rule, however, passing an array by reference to a function that does not modify its contents is actually slower than passing it by value, since PHP must create a set of structures that it uses to maintain the reference. Because PHP uses a lazy-copy mechanism (also called copy-on-write) that does not actually create a copy of a variable until it is modified, passing an array by value is a very fast and safe method of sharing an array with a function and, therefore answer E is correct.
    1. Report
  10. Question: What will the following script output?
    <?php
    function sort_my_array ($array){
      return sort ($array);
    }
    $a1 = array (3, 2, 1);
    var_dump (sort_my_array (&$a1));
    ?>

    A
    NULL

    B
    0 => 1, 1 => 2, 2 => 3

    C
    An invalid reference error

    D
    2 => 1, 1 => 2, 0 => 3

    E
    bool (true)

    Note: The correct answer is E. The sort function works directly on the array passed (by reference) to it, without creating a copy and returning it. Instead, it returns the Boolean value True to indicate a successful sorting operation (or False to indicate an error). Note that this example passes the $a1 array to sort_my_array() by reference; this technique is deprecated and the function should be re-declared as accepting values by reference instead.
    1. Report
Copyright © 2024. Powered by Intellect Software Ltd