For Complete blog visit Zend Certified Engineer ZCE PHP Basics Mock Test
Answers for the above mentioned blog are
1. Looking at the answers, the only one that makes sense for every blank is B. PHP is a
scripting language based on the Zend Engine that is usually embedded in HTML code. As
such, it is primarily used to develop HTML documents, although it can be used just as nicely
to develop other types of documents, such as XML.
2. While tags such as <% %> and <?= ?> are often forgotten in PHP programming, they are valid
ways to delimit a PHP code block. The <! and !> tags, however, are not valid and, therefore,
the correct answer is D. Keep in mind, in any case, that some of these tags are not always
available, depending on how the php.ini file on which the PHP interpreter runs is configured.
3. 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.
4. 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.
5. 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.
6. 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).
7. 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).
8. 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.
Besides, even if the random number generator had not been seeded, the script would have
still outputted 49 pseudo-random characters—the same ones every time. The $array variable,
though a string, can be accessed as an array, in which case the individual characters
corresponding to the numeric index used will be returned. Finally, the for loop starts from 1
and continues until $i is less than 50—for a total of 49 times.
9. 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.
10. 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)";
}
?>
11. 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.
12. 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.
13. 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.
14. 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.
15. 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.
16. 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.
17. The right answer here is the exclusive-or (xor) operator.
18. 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.
19. The correct answers are A and C. In Answer A, the pow function is used to calculate 22,
which corresponds to 4. In Answer C, the left bitwise shift operator is used to shift the value
of $a by two bits to the left, which corresponds to a multiplication by 4.
20. The only answer that really fits the bill is A. A script doesn’t necessarily terminate when it
reaches the end of any file other than the main one—so the “current” file could be externally
included and not cause the script to terminate at its end. As far as PHP and Apache crashes,
they can hardly be considered “clean” ways to terminate a script.
Answers for the above mentioned blog are
1. Looking at the answers, the only one that makes sense for every blank is B. PHP is a
scripting language based on the Zend Engine that is usually embedded in HTML code. As
such, it is primarily used to develop HTML documents, although it can be used just as nicely
to develop other types of documents, such as XML.
2. While tags such as <% %> and <?= ?> are often forgotten in PHP programming, they are valid
ways to delimit a PHP code block. The <! and !> tags, however, are not valid and, therefore,
the correct answer is D. Keep in mind, in any case, that some of these tags are not always
available, depending on how the php.ini file on which the PHP interpreter runs is configured.
3. 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.
4. 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.
5. 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.
6. 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).
7. 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).
8. 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.
Besides, even if the random number generator had not been seeded, the script would have
still outputted 49 pseudo-random characters—the same ones every time. The $array variable,
though a string, can be accessed as an array, in which case the individual characters
corresponding to the numeric index used will be returned. Finally, the for loop starts from 1
and continues until $i is less than 50—for a total of 49 times.
9. 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.
10. 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)";
}
?>
11. 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.
12. 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.
13. 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.
14. 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.
15. 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.
16. 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.
17. The right answer here is the exclusive-or (xor) operator.
18. 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.
19. The correct answers are A and C. In Answer A, the pow function is used to calculate 22,
which corresponds to 4. In Answer C, the left bitwise shift operator is used to shift the value
of $a by two bits to the left, which corresponds to a multiplication by 4.
20. The only answer that really fits the bill is A. A script doesn’t necessarily terminate when it
reaches the end of any file other than the main one—so the “current” file could be externally
included and not cause the script to terminate at its end. As far as PHP and Apache crashes,
they can hardly be considered “clean” ways to terminate a script.
No comments:
Post a Comment