1. 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
  2. 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
  3. 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
  4. Question: What will be the output of the following script?
    <?php
    $result = '';
    function glue ($val){
      global $result;
      $result .= $val;
    }
    $array = array ('a', 'b', 'c', 'd');
    array_walk ($array, 'glue');
    echo $result;
    ?>

    A
    abcd

    B
    glue

    C
    Array

    D
    abcdabcd

    E
    none

    Note: The array_walk function executes a given callback function for every element of an array. Therefore, this script will cause the glue function to concatenate all the elements of the array and output abcd.
    1. Report
  5. Question: What will the following script output?
    <?php
    $array = array (1, 2, 3, 5, 8, 13, 21, 34, 55);
    $sum = 0;
    for ($i = 0; $i < 5; $i++) {
      $sum += $array[$array[$i]];
    }
    echo $sum;
    ?>

    A
    78

    B
    19

    C
    NULL

    D
    5

    E
    0

    Note: This question is designed to test your ability to analyze a complex script more than your understanding of arrays. You may think it too convoluted—but we’ve all been faced with the not-so-pleasant task of debugging someone else’s code, and compared to some of the scripts we’ve seen, this is actually quite simple. The script simply cycles through the for loop five times, each time adding to $sum the value of the element of $array whose key is equal to the value of the element of $array whose key is equal to $i. It might sound a bit like a high-tech variation of “how much wood would a wood chuck chuck,” but if you step through the code manually, you’ll find that, when $i is zero, then $array[$array[$i]] becomes $array[$array[0]], or $array[1], that is, 2. Applied to all the iterations of the for loop, the resulting total is 78.
    1. Report
Copyright © 2024. Powered by Intellect Software Ltd