This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “For Loops – 3”.
1. What will be the output of the following PHP code?
<?php
for ($i = 0; 0; $i++)
{
print"i";
}
?>
a) infinite loop
b) 0
c) no output
d) error
View Answer
Explanation: The condition of the loop is always false 0.
2. What will be the output of the following PHP code?
<?php
for ($i = 0; $i < 5; $i++)
{
for ($j = $i; $j > 0; $i--)
print $i;
}
?>
a) infinite loop
b) 0 1 2 3 4 5
c) 0 1 0 1 2 0 1 2 3 0 1 2 3 4 0 1 2 3 4 5 0 1 2 3 4 5
d) no output
View Answer
Explanation: In the second loop j value is not being changed.
3. What will be the output of the following PHP code?
<?php
for ($i = 0; $i < 5; $i++)
{
for ($j = $i;$j > $i; $i--)
print $i;
}
?>
a) infinite loop
b) 0 1 2 3 4 5
c) 0 1 0 1 2 0 1 2 3 0 1 2 3 4 0 1 2 3 4 5 0 1 2 3 4 5
d) no output
View Answer
Explanation: The second loop does not execute as the check condition is always false.
4. What will be the output of the following PHP code?
<?php
$user = array("Ashley", "Bale", "Shrek", "Blank");
for ($x = 0; $x < count($user); $x++)
{
if ($user[$x] == "Shrek")
continue;
printf ($user[$x]);
}
?>
a) AshleyBaleBlank
b) AshleyBale
c) AshleyBaleShrek
d) No output
View Answer
Explanation: Only the Shrek is skipped due to the continue statement.
5. What will be the output of the following PHP code?
<?php
$user = array("Ashley", "Bale", "Shrek", "Blank");
for ($x=0; $x < count($user) - 1; $x++)
{
if ($user[$x++] == "Shrek")
continue;
printf ($user[$x]);
}
?>
a) AshleyBaleBlank
b) Bale
c) AshleyShrek
d) BaleBlank
View Answer
Explanation: Only Bale is printed as $x++ is done before printing and then checked.
6. What will be the output of the following PHP code?
<?php
$user = array("Ashley", "Bale", "Shrek", "Blank");
for ($x = 0; $x < count($user); $x)
{
if ($user[$x++] == "Shrek")
continue;
printf ($user[$x]);
}
?>
a) AshleyBaleBlank
b) BaleShrek
c) AshleyBlank
d) Bale
View Answer
Explanation: x is incremented only inside loop i the if condition.
7. What will be the output of the following PHP code?
<?php
for ($i = 0; $i % ++$i; $i++)
{
print"i";
}
?>
a) error
b) infinite loop
c) no output
d) 0
View Answer
Explanation: Loop condition is true as i%(i+1) is a float non zero value in php.
8. What will be the output of the following PHP code?
<?php
for ($i = 0; $i < 5; $i++)
{
for(; $i < 5; $i++)
print"i";
}
?>
a) iiiii
b) infinite loop
c) iiiiiiiiiiiiiiiiiiiiiiiii
d) no output
View Answer
Explanation: The i value is changed in the inner loop and reaches five, thus does not execute the second outer loop.
9. What will be the output of the following PHP code?
<?php
$a = array("hi", "hello", "bye");
foreach ($a as $value)
{
if (count($a) == 2)
print $value;
}
?>
a) hihellobye
b) infinite loop
c) hihello
d) no output
View Answer
Explanation: As count($a) returns 3 the condition is always false.
10. What will be the output of the following PHP code?
<?php
$a = array("hi", "hello", "bye");
for (;count($a) < 5;)
{
if (count($a) == 3)
print $a;
}
?>
a) ArrayArrayArrayArrayArrayArray….infinitely
b) (“hi”,”hello”,”bye”)(“hi”,”hello”,”bye”)(“hi”,”hello”,”bye”)(“hi”,”hello”,”bye”)…infinitely
c) hihellobyehihellobyehihellobyehihellobyehihellobyehihellobye…..infinitely
d) no output
View Answer
Explanation: As count($a) returns 3 the condition is always true, thus it prints $a, which returns its data type.
Sanfoundry Global Education & Learning Series – PHP Programming.
To practice all areas of PHP Programming, here is complete set of 1000+ Multiple Choice Questions and Answers on PHP.
- Apply for Programming Internship
- Check Information Technology Books
- Check PHP Books
- Practice MCA MCQs
- Practice Programming MCQs