1. Question: Which language construct can best represent the following series of if conditionals?
    <?php
    if($a == 'a') {
      somefunction();
    } else if ($a == 'b') {
      anotherfunction();
    } else if ($a == 'c') {
      dosomething();
    } else {
      donothing();
    }
    ?>

    A
    A switch statement without a default case

    B
    A recursive function call

    C
    A while statement

    D
    It is the only representation of this logic

    E
    A switch statement using a default case

    Note: A series of if…else if code blocks checking for a single condition as above is a perfect place to use a switch statement:
    <?php
    switch($a) {
    case 'a':
     somefunction();
    break;
    case 'b':
     anotherfunction();
    break;
    case 'c':
     dosomething();
    break;
    default:
     donothing();
    }
    ?>
    Because there is a catch-all else condition, a default case must also be provided for that situation. Answer E is correct.
    1. Report
  2. Question: What is the best way to iterate through the $myarray array, assuming you want to modify the value of each element as you do?
    <?php
    $myarray = array ("My String",
    "Another String",
    "Hi, Mom!");
    ?>

    A
    Using a for loop

    B
    Using a foreach loop

    C
    Using a while loop

    D
    Using a do…while loop

    E
    There is no way to accomplish this goal

    Note: Normally, the foreach statement is the most appropriate construct for iterating through an array. However, because we are being asked to modify each element in the array, this option is not available, since foreach works on a copy of the array and would therefore result in added overhead. Although a while loop or a do…while loop might work, because the array is sequentially indexed a for statement is best suited for the task, making Answer A correct:
    <?php
    $myarray = array ("My String", "Another String", "Hi, Mom!");
    for($i = 0; $i < count($myarray); $i++)
    {
    $myarray[$i] .= " ($i)";
    }
    ?>
    1. Report
  3. Question: Consider the following segment of code:
    <?php
    define("STOP_AT", 1024);
    $result= array();
    /* Missing code */
    {
       $result[] = $idx;
    }
    print_r($result);
    ?>
    What should go in the marked segment to produce the following array output? Array { [0] => 1 [1] => 2 [2] => 4 [3] => 8 [4] => 16 [5] => 32 [6] => 64 [7] => 128 [8] => 256 [9] => 512 }

    A
    foreach($result as $key => $val)

    B
    while($idx *= 2)

    C
    for($idx = 1; $idx < STOP_AT; $idx *= 2)

    D
    for($idx *= 2; STOP_AT >= $idx; $idx = 0)

    E
    while($idx < STOP_AT) do $idx *= 2

    Note: As it is only possible to add a single line of code to the segment provided, the only statement that makes sense is a for loop, making the choice either C or D. In order to select the for loop that actually produces the correct result, we must first of all revisit its structural elements. In PHP, for loops are declared as follows: for (<init statement>; <continue until statement>; <iteration statement>) where the <init statement> is executed prior to entering the loop. The for loop then begins executing the code within its code block until the <continue until> statement evaluates to False. Every time an iteration of the loop is completed, the <iteration statement> is executed. Applying this to our code segment, the correct for statement is: for ($idx = 1; $idx < STOP_AT; $idx *= 2) or answer C.
    1. Report
  4. Question: Choose the appropriate function declaration for the user-defined function is_leap(). Assume that, if not otherwise defined, the is_leap function uses the year 2000 as a default value:
    <?php
    /* Function declaration here */
    {
      $is_leap = (!($year %4) && (($year % 100) ||
      !($year % 400)));
      return $is_leap;
    }
    var_dump(is_leap(1987)); /* Displays false */
    var_dump(is_leap()); /* Displays true */
    ?>

    A
    function is_leap($year = 2000)

    B
    is_leap($year default 2000)

    C
    function is_leap($year default 2000)

    D
    function is_leap($year)

    E
    function is_leap(2000 = $year)

    Note: Of the five options, only two are valid PHP function declarations (A and D). Of these two declarations, only one will provide a default parameter if none is passed—Answer A.
    1. Report
  5. Question: What is the value displayed when the following is executed? Assume that the code was executed using the following URL: testscript.php?c=25
    <?php
    function process($c, $d = 25){
       global $e;
       $retval = $c + $d - $_GET['c'] - $e;
       return $retval;
    }
    $e = 10;
    echo process(5);
    ?>

    A
    25

    B
    -5

    C
    10

    D
    5

    E
    0

    Note: This question is designed to test your knowledge of how PHP scopes variables when dealing with functions. Specifically, you must understand how the global statement works to bring global variables into the local scope, and the scope-less nature of superglobal arrays such as $_GET, $_POST, $_COOKIE, $_REQUEST and others. In this case, the math works out to 5 + 25 - 25 – 10, which is -5, or answer B.
    1. Report
  6. Question: Consider the following script:
    <?php
    function myfunction($a, $b = true)
    {
      if($a && !$b) {
        echo "Hello, World!\n";
      }
    }
    $s = array(0 => "my",
    1 => "call",
    2 => '$function',
    3 => ' ',
    4 => "function",
    5 => '$a',
    6 => '$b',
    7 => 'a',
    8 => 'b',
    9 => '');
    $a = true;
    $b = false;
    /* Group A */
    $name = $s[?].$s[?].$s[?].$s[?].$s[?].$s[?];
    /* Group B */
    $name(${$s[?]}, ${$s[?]});
    ?>
    Each ? in the above script represents an integer index against the $s array. In order to display the Hello, World! string when executed, what must the missing integer indexes be?

    A
    Group A: 4,3,0,4,9,9 Group B: 7,8

    B
    Group A: 1,3,0,4,9,9 Group B: 7,6

    C
    Group A: 1,3,2,3,0,4 Group B: 5,8

    D
    Group A: 0,4,9,9,9,9 Group B: 7,8

    E
    Group A: 4,3,0,4,9,9 Group B: 7,8

    Note: Functions can be called dynamically by appending parentheses (as well as any parameter needed) to a variable containing the name of the function to call. Thus, for Group A the appropriate index combination is 0, 4, 9, 9, 9, 9, which evaluates to the string myfunction. The parameters, on the other hand, are evaluated as variables dynamically using the ${} construct. This means the appropriate indexes for group B are 7 and 8, which evaluate to ${'a'} and ${'b'}—meaning the variables $a and $b respectively. Therefore, the correct answer is D.
    1. Report
  7. Question: Run-time inclusion of a PHP script is performed using the ________ construct, while compile-time inclusion of PHP scripts is performed using the _______ construct.

    A
    include_once, include

    B
    require, include

    C
    require_once, include

    D
    include, require

    E
    All are correct

    Note: In recent versions of PHP, the only difference between require() (or require_once()) and include() (or include_once()) is in the fact that, while the former will only throw a warning and allow the script to continue its execution if the include file is not found, the latter will throw an error and halt the script. Therefore, Answer E is correct.
    1. Report
  8. Question: Under what circumstance is it impossible to assign a default value to a parameter while declaring a function?

    A
    When the parameter is Boolean

    B
    When the function is being declared as a member of a class

    C
    When the parameter is being declared as passed by reference

    D
    When the function contains only one parameter

    E
    Never

    Note: When a parameter is declared as being passed by reference you cannot specify a default value for it, since the interpreter will expect a variable that can be modified from within the function itself. Therefore, Answer C is correct.
    1. Report
  9. Question: The ____ operator returns True if either of its operands can be evaluated as True, but not both. Your Answer: _______

    A
    xor

    B
    nor

    C
    or

    D
    and

    E
    !and

    Note: The right answer here is the exclusive-or (xor) operator.
    1. Report
  10. Question: How does the identity operator === compare two values?

    A
    It converts them to a common compatible data type and then compares the resulting values

    B
    It returns True only if they are both of the same type and value

    C
    If the two values are strings, it performs a lexical comparison

    D
    It bases its comparison on the C strcmp function exclusively

    E
    It converts both values to strings and compares them

    Note: The identity operator works by first comparing the type of both its operands, and then their values. If either differ, it returns False—therefore, Answer B is correct.
    1. Report
Copyright © 2024. Powered by Intellect Software Ltd