Zend Exam Type Questions II

It’s funny how a simple question can trick you to the most simple of the errors. Take a look at this question:

<?php
$oranges = 10;
$apples = 5;
$string = "I have %d apples and %d oranges";
???????
?>

What could be placed in place of ?????? to output the string:
I have 5 apples and 10 oranges

Answers: (choose 2)

A

str_format($string, $apples, $oranges);

B

print($string, $apples, $oranges);

C

printf($string, $apples, $oranges);

D

print sprintf($apples, $oranges);

E

sprintf($string, $oranges, $apples);

There are 2 correct answers for this and as you know, printf and sprintf are the only PHP functions that can format a string. So the answers C and E would be the correct answers, right!? Wrong. The catch in this question is that sprintf only formats the string, it does not print, making the correct answers C and D.

This is another question that is also interesting. I love complex (or curly) variables passing for strings. It make writing simple sql statements when inside a class way much easier. I have came accross this question during a mock exam:

What variable reference would go in the spots indicated by ????? in the code segment below?

<?php
$msg = "The Quick Brown Foxed Jumped Over the Lazy Dog";
$state = true;
$retval = "";
for($i = 0; (isset(??????)); $i++) {
       if($state) {
               $retval .= strtolower(?????);
        } else {
               $retval .= strtoupper(?????);
        }
        $state = !$state;
}
print $retval;?>

Answer…

A

$msg{$i}

B

ord($msg);

C

chr($msg);

D

substr($msg, $i, 2);

Strings can be accessed as an array of characters (funny how you not always remember this kind of stuff). You can use the [$pos] to access a character in a given position of a string. The same is true for the {$pos} making the A as the correct answer. This is part of PHP string access interface that uses the [] and {} as access formats for the string. The same {} that you use for a complex string. Just a note,  $string{$pos} will be removed from PHP6 and passing the position of the character is required.

Advertisements

About mcloide

Making things simpler, just check: http://www.mcloide.com View all posts by mcloide

9 responses to “Zend Exam Type Questions II

  • Mikolaj

    Regarding first question you are wrong, answer D is incorrect as it prints 5. There is only one correct answer which is C. There is also another mistake in mockup test in this question:

    Consider the following script:
    <?php

    $string = “I like ‘PHP’ & I think it is \”cool\”“;

    var_dump(htmlentities($string, ENT_QUOTES));
    var_dump(print htmlspecialchars($string));

    ?>
    In this script, do the two var_dump() calls produce the same string? Why or Why Not?

    Anwser(s) Choose: 1
    No, the htmlentities() call will translate quotes while the htmlspecialchars() call will not
    No, htmlentites() translates symbols to their HTML entity
    equivalents while htmlspecialchars() only does quotes
    No, the htmlentites() call won’t translate quotes to HTML entities while the htmlspecialchars() call will
    Yes, htmlspecialchars() and htmlentities() with the ENT_QUOTES constants produce the same result.

    look at var_dump(print htmlspecialchars($string)); and you may notice print which always return true so none of answers are correct.

  • mcloide

    The question asks you to select 2 answers, therefore you have to select C and D to correctly answer the first question and this is the whole trick of the question. I have copied the question correctly from the test, but I do agree with you that the question needs an errata, it needs to correct the D answer to “print sprintf($string, $apples, $oranges)” in order to work correctly and for the question to make sense.

    Answering your question it really needs an errata. they are asking about the var_dump results and not about the strings. The var_dumps will produce an string and int 1, but both strings will be the same.

    Thanks for your help and keep coming back, discussing questions help to learn and improve more to the Zend exam.

  • Davis Briggs

    Nope, there’s no correct answer for that question. Look:

    <?php

    $string = “I like ‘PHP’ & I think it is \”cool\”“;

    htmlentities($string, ENT_QUOTES);
    /**
    * Outputs:
    * <b>I like 'PHP' & I think it is "cool"</b>
    */

    htmlspecialchars($string);
    /**
    * Outputs:
    * <b>I like ‘PHP’ & I think it is "cool"</b>
    */

    ?>

    So I *guess* the closest correct answer is “D” (The ‘Yes’ answer).

    *sigh*

  • jpablobr

    This is my output:

    string(78) “I like ‘PHP’ & I think it is “cool”

    I like ‘PHP’ & I think it is “cool”int(1)

    So, I think the correct answer would be:

    No, the htmlentites() call won’t translate quotes to HTML entities while the htmlspecialchars() call will

  • mcloide

    You got look on the var_dump result type for understanding this question.

    var_dump(htmlentities($string, ENT_QUOTES));
    var_dump(print htmlspecialchars($string));

    The first one returns a type string with 78 chars and the second one will return integer 1 since print will always return true.

    Both results are different, no matter if what is being printed is the same thing.

    What Mikolaj has posted before is correct, this question does not have a correct answer, but is a good type of question for the exam (if it had a correct answer) because makes you focus on the result of htmlspecialchars and htmlentities and not focus on the result of var_dump.

  • jpablobr

    Hi I’m totally confused with this question… any ideas?

    Consider the following PHP string representing an SQL statement:
    $query = “UPDATE users SET password=’$password’ WHERE username=’$username'”;
    Which of the following values for $username or $password would change the behavior of this query when executed?

    Answer…
    None of the above
    $username = “foobar\’ WHERE username=’admin'”;
    $password = “foobar’ WHERE username=’admin’ –:”;
    $username = “foobar\’ WHERE username=’admin'”;
    $password = “\”foobar\” WHERE username=\”admin\””;

    Thanks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: