This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “While Loops”.
1. What will be the output of the following PHP code?
<?php
while()
{
print "hi";
}
?>
a) infinite loop
b) hi
c) no output
d) error
View Answer
Explanation: The while loop cannot be defined without a condition.
2. What will be the output of the following PHP code?
<?php
do
{
print "hi";
}
while(0);
print "hello";
?>
a) infinite loop
b) hihello
c) hello
d) error
View Answer
Explanation: The do while loop executes at least once as the condition is in the while loop.
3. What will be the output of the following PHP code?
<?php
$i = 0
do
{
print "hi";
$i++;
}
while ($i != 3);
?>
a)
hi hi
b) hi
c)
hi hi hi hi
d) no output
View Answer
Explanation: The check happens after the increment,thus it prints until i = 4.
4. What will be the output of the following PHP code?
<?php
$i = 0
while ($i != 3)
{
print "hi";
$i++;
}
?>
a)
hi hi
b)
hi hi hi
c)
hi hi hi hi
d) no output
View Answer
Explanation: The check happens before the increment, thus it prints until i = 3.
5. What will be the output of the following PHP code?
<?php
$i = 0
while ($i < 3)
{
print "hi";
$i--;
}
print "hello"
?>
a)
hi hi hello
b)
hi hi hi hello
c)
hi hi hi hi hello
d) infinite loop
View Answer
Explanation: There is no increment of i making it infinite.
6. What will be the output of the following PHP code?
<?php
$i = 0
while ($i < 3)
{
$i++;
}
print $i;
?>
a) 2
b) 3
c) 0
d) 1
View Answer
Explanation: The increment happens and then the check happens.
7. What will be the output of the following PHP code?
<?php
$i = 0
do
{
$i++;
}
while ($i < 3);
print $i;
?>
a) 2
b) 3
c) 0
d) 1
View Answer
Explanation: The increment happens and then the check happens.
8. What will be the output of the following PHP code?
<?php
$i = 0
while ($i++)
{
print $i;
}
print $i;
?>
a) 0
b) infinite loop
c) 01
d) 1
View Answer
Explanation: As it is a post increment, it checks and then does not enter the loop, thus prints only 1.
9. What will be the output of the following PHP code?
<?php
$i = "";
while($i)
{
print "hi";
}
print "hello";
?>
a) hello
b) infinite loop
c) hihello
d) error
View Answer
Explanation: $i is null string which evaluates to false in the while loop and hence the statements inside the while loop won’t be executed.
10. What will be the output of the following PHP code?
<?php
$i = "";
while ($i)
{
print "hi";
}
while($i < 8)
$i++;
print "hello";
?>
a) hi is printed 8 times, hello 7 times and then hi 2 times
b) hi is printed 10 times, hello 7 times
c) hi is printed once, hello 7 times
d) hello
View Answer
Explanation: $i is null string which evaluates to false in the 1st while loop and hence the statements inside the 1st while loop won’t be executed. In the 2nd while loop, it increments 8 times, and then it prints “hello” once.
More MCQs on PHP While Loops:
Sanfoundry Global Education & Learning Series – PHP Programming.
To practice all questions on PHP Programming, here is complete set of 1000+ Multiple Choice Questions and Answers on PHP.
- Check MCA Books
- Check Information Technology Books
- Practice Programming MCQs
- Apply for Programming Internship
- Practice MCA MCQs