PHP Control Flow Quiz

Quiz 9 14 questions

Prerequisites for the quiz

  1. Entire PHP Control Flow unit

Are you ready?

14 questions to solve

Instructions
  1. This quiz goes to full-screen once you press the Start button.
  2. At the end of the quiz, you are able to review all the questions that you answered wrong and see their explanations.

Syntactically, the header of a for loop can be left empty as shown in the following code:

<?php

for (;;) {
   /* Code here */
}
True or false?
The header of a for loop, which simply refers to the pair of parentheses (()) after the for keyword, can indeed be left empty given that we make sure to include the semicolons (;) as shown in the code above. The correct choice, therefore, is (A). For more info, refer to PHP Control Flow — for.
What is the problem in the following code?
<?php

$a = (int) fgets(STDIN);

if ($a === 0) {
   echo 'Unsigned';
}
elseif ($a < 0) {
   echo 'Signed (negative)';
}
else {
   echo 'Signed (positive)';
}
The code is absolutely fine. elseif is also a reserved keyword in PHP. Hence, the correct choice is (C). For more info, refer to PHP Control Flow — elseif.
If programmed incorrectly, a loop might run infinitely. True or false?
Definitely yes. An incorrectly-programmed loop might never have its termination condition fulfilled and, thus, keep on executing forever. The correct choice is (A).
We want to set up a loop where, regardless of the loop's condition, its body must be executed at least once. Which of the following loop should we use?
The do...while loop executes its body at least once just as required here. Hence, the correct choice is (D). For more info, refer to PHP Control Flow — do...while.
How to exit out of a loop?
The break keyword is used to exit out of a loop. This goes with choice (B). For more info, refer to PHP Control Flow — break and continue.
After the execution of the following code, what would be the value of $nums[0]?
<?php

$nums = [1, 2, 3];

foreach ($nums as $num) {
   $num = $num + 1;
}
In each iteration of the loop, $num holds a copy of the current element of the $nums array. Likewise, modifying it won't have any effect on $nums. Hence, the correct choice is (A). For more info, refer to PHP Control Flow — foreach.
How many times does the following loop run?
<?php

$i = 0;
for (; $i < 3; $i++) {
   // ...
}
It's completely alright to skip the initialization part in the header of a for loop, as done above. We start with $i = 0. With this, the condition $i < 3 gets fulfilled exactly 3 times and that's the number of times the loop executes. Hence, the correct choice is (B). For more info, refer to PHP Control Flow — for.
How many times does the following loop run?
<?php

for ($i = 1; $i >= 4; $i++) {
   echo $i;
}
By virtue of the initialization $i = 1, the condition $i >= 4 becomes 1 >= 4 which is false. Likewise, the loop doesn't execute at all. This goes with choice (A). For more info, refer to PHP Control Flow — for.
What is the problem in the following code?
<?php

$cities = ['Tokyo', 'Beijing', 'Amsterdam', 'London'];

foreach ($cities as $key => &$city) {
   $city = strtoupper($city);
}
The code shown here is absolutely fine; there's no problem in it. The $city variable, in each iteration of the foreach loop, acts as an alias of the current element. Hence, the correct choice is (C). For more info, refer to PHP Control Flow — foreach.
What does the following code output?
<?php

$x = 10;

if (is_int($x)) {
   if ($x % 2 === 0) echo 1;
   else echo 2;
}
else {
   echo 3;
}
$x = 10 is an integer likewise is_int($x) yields true. Furthermore, because it is an even integer, $x % 2 === 0 also yields true, ultimately echoing 1. Hence, the correct choice is (A). For more info, refer to PHP Control Flow — if...else.
What does the following code output?
<?php

$x = 30;

if ($x % 2 === 0 && $x % 3 === 0) {
   echo 1;
}
else {
   echo 2;
}
Since $x = 30 is divisible by both 2 and 3, both the aforementioned conditions yield true and, likewise, the logical AND (&&) operation also yields true, ultimately outputting 1. Hence, the correct choice is (A). For more info, refer to PHP Control Flow — if...else.
Is the following code valid or not?
<?php

$i = 0;
for ($i < 5; $i++) {
   echo $i;
}
It's a syntactic requirement to have exactly 2 semicolons (;) in the header of a for loop. However, in the code above, this doesn't hold and, likewise, it's invalid. This goes with choice (B). For more info, refer to PHP Control Flow — for.
What does the following code output?
<?php

$x = 0;

while ($x === 0 || $x === 1) {
   echo x, ' ';
   $x += 1;
}
Initially, $x === 0 || $x === 1 gets fulfilled since $x = 0. Likewise, the loop executes for the first time. Then $x is incremented to 1. Next up, $x === 0 || $x === 1 gets fulfilled again as $x = 1, thereby executing the loop a second time. Then $x becomes 2 and that gets the loop's condition to fail.

Altogether, we get the output 0 1, and this goes with choice (B). For more info, refer to PHP Control Flow — while.
What does the following code output?
<?php

$fruit = 'orange';

switch ($fruit) {
    case "orange":
        echo "Citrus\n";
    case "orange\n":
        echo "Sweet\n";
}
$fruit matches the first case, i.e. case "orange", in the switch statement and so the text 'Citrus' gets output. Thereafter, by virtue of the fallthrough behavior of switch, execution tips over into the second case block, ultimately outputting the text 'Sweet' as well.

Overall, we get the output shown in choice (C). For more info, refer to PHP Control Flow — switch.

"I created Codeguage to save you from falling into the same learning conundrums that I fell into."

— Bilal Adnan, Founder of Codeguage