1. Question: Which of the following is not valid PHP code?

    A
    $_10

    B
    ${“MyVar”}

    C
    &$something

    D
    $10_somethings

    E
    $aVaR

    Note: PHP variables always start with a dollar sign and are a sequence of characters and numbers within the Latin alphabet, plus the underscore character. ${"MyVar"} is a valid variable name that simply uses a slightly less common naming convention, while &$something is a reference to the $something variable. Variables, however cannot start with numbers, making $10_somethings invalid and Answer D correct.
    1. Report
  2. Question: What is displayed when the following script is executed?
    <?php
    define(myvalue, "10");
    $myarray[10] = "Dog";
    $myarray[] = "Human";
    $myarray['myvalue'] = "Cat";
    $myarray["Dog"] = "Cat";
    print "The value is: ";
    print $myarray[myvalue]."\n";
    ?>

    A
    The value is: Dog

    B
    The value is: Cat

    C
    The value is: Human

    D
    The value is: 10

    E
    The value is: 10

    Note: The important thing to note here is that the $myarray array’s key value is being referenced without quotes around it. Because of this, the key being accessed is not the myvalue string but the value represented by the myvalue constant. Hence, it is equivalent to accessing $myarray[10], which is Dog, and Answer A is correct.
    1. Report
  3. Question: What is the difference between print() and echo()?

    A
    print() can be used as part of an expression, while echo() can’t

    B
    echo() can be used as part of an expression, while print() can’t

    C
    echo() can be used in the CLI version of PHP, while print() can’t

    D
    print() can be used in the CLI version of PHP, while echo() can’t

    E
    There’s no difference: both functions print out some text!

    Note: Even though print() and echo() are essentially interchangeable most of the time, there is a substantial difference between them. While print() behaves like a function with its own return value (although it is a language construct), echo() is actually a language construct that has no return value and cannot, therefore, be used in an expression. Thus, Answer A is correct.
    1. Report
  4. Question: What is the output of the following script?
    <?php
    $a = 10;
    $b = 20;
    $c = 4;
    $d = 8;
    $e = 1.0;
    $f = $c + $d * 2;
    $g = $f % 20;
    $h = $b - $a + $c + 2;
    $i = $h << $c;
    $j = $i * $e;
    print $j;
    ?>

    A
    128

    B
    42

    C
    242.0

    D
    256

    E
    342

    Note: Other than the simple math, the % operator is a modulus, which returns whatever the remainder would be if its two operands were divided. The << operator is a left-shift operator, which effectively multiplies an integer number by powers of two. Finally, the ultimate answer is multiplied by a floating point and, therefore, its type changes accordingly. However, the result is still printed out without any fractional part, since the latter is nil. The final output is 256 (Answer D).
    1. Report
  5. Question: Which values should be assigned to the variables $a, $b and $c in order for the following script to display the string Hello, World!?
    <?php
    $string = "Hello, World!";
    $a = ?;
    $b = ?;
    $c = ?;
    if($a) {
      if($b && !$c) {
        echo "Goodbye Cruel World!";
      } else if(!$b && !$c) {
        echo "Nothing here";
      }
    } else {
       if(!$b){
         if(!$a && (!$b && $c)) {
            echo "Hello, World!";
         } else {
           echo "Goodbye World!";
         }
      } else {
        echo "Not quite.";
      }
    }
    ?>

    A
    False, True, False

    B
    True, True, False

    C
    False, True, True

    D
    False, False, True

    E
    True, True, True

    Note: Following the logic of the conditions, the only way to get to the Hello, World! string is in the else condition of the first if statement. Thus, $a must be False. Likewise, $b must be False. The final conditional relies on both previous conditions ($a and $b) being False, but insists that $c be True (Answer D).
    1. Report
  6. Question: What will the following script output?
    <?php
    $array = '0123456789ABCDEFG';
    $s = '';
    for ($i = 1; $i < 50; $i++) {
      $s .= $array[rand(0,strlen ($array) - 1)];
    }
    echo $s;
    ?>

    A
    A string of 50 random characters

    B
    A string of 49 copies of the same character, because the random number generator has not been initialized

    C
    A string of 49 random characters

    D
    Nothing, because $array is not an array

    E
    A string of 49 ‘G’ characters

    Note: The correct answer is C. As of PHP 4.2.0, there is no need to initialize the random number generator using srand() unless a specific sequence of pseudorandom numbers is sought.
    1. Report
  7. 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
  8. 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
  9. 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
  10. 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
Copyright © 2024. Powered by Intellect Software Ltd