PHP Loop MCQs

PHP Loop MCQs : This section focuses on "Loop" in PHP. These Multiple Choice Questions (mcq) should be practiced to improve the PHP skills required for various interviews (campus interview, walk-in interview, company interview), placements, entrance exams and other competitive examinations.

1. PHP supports ____ types of looping techniques?

A. 2
B. 3
C. 4
D. 5

View Answer


2. How many main parameter are used in for loop?

A. 2
B. 3
C. 4
D. 1

View Answer


3.  do-while loop is an _____ control loop ?

A. exit
B. exist
C. easy
D. entry

View Answer


4. while loop is an _____ control loop ?

A. exit
B. exist
C. easy
D. entry

View Answer


5. foreach loop is used to iterate over ____?

A. number
B. object
C. function
D. array

View Answer


6. Which loop evaluates the condition expression as Boolean, if it is true, it executes the statements and when it is false it will terminate?

A. For loop
B. while loop
C. do-while loop
D. All of the above

View Answer


7. What will be the output of the following PHP code?

<?php  
  
for ($num = 1; $num <= 10; $num += 2) { 
    echo "$num "; 
}  
  
?>

A. 1 3 5 7 9
B. 1 2 3 4 5
C. 9 7 5 3 1
D. Error

View Answer


8. What will be the output of the following PHP code?

<?php  
$num = 20; 
  
while ($num < 12) { 
    $num += 2; 
    echo $num, "\n"; 
} 
?>

A. Error
B. No Output
C. infinite loop
D. Only one garbage value

View Answer


9. What will be the output of the following PHP code?

<?php   
$num = 2; 
do { 
    $num += 2; 
    echo $num, "\n"; 
} while ($num < 0); 
?>

A. Error
B. No Output
C. infinite loop
D. 4

View Answer


10. What will be the output of the following PHP code?

<?php   
  $arr = array (10, 20, 30); 
    foreach ($arr as $val) {  
        echo "$val1 \n"; 
    }  
?>

A. 10 20 30
B. No Output
C. 10
D. undefined variable

View Answer


11. What will be the output of the following PHP code ?

<?php
    do
    {
          print "LFC";
    }
    while(10);
    print "I LOVE WEBSITE";
?>

A. LFC
B. I LOVE WEBSITE
C. LFC I LOVE WEBSITE
D. infinite loop

View Answer


12. What will be the output of the following PHP code ?

<?php
   $a;
   for ($a = -3; $a < -5; ++$a)
   {
        print $x++;
   }
?>

A. -3-4-5
B. -3-4
C. No Output
D. infinite loop

View Answer


13. What will be the output of the following PHP code ?

<?php
for ($a = 1; $a <= 10; $a++){
    for ($b = 1; $b <= 5; $b++){
        print "LFC
";
    }
}
?>

A. Print "LFC" 10 times
B. Print "LFC" 50 times
C. Print "LFC" 5 times
D. Print "LFC" 1 times

View Answer


14. What will be the output of the following PHP code ?

<?php
for($num = 10; $num<=20; print ++$num)
  {
      print ++$num;
  }
?>

A. 111213141516171819202122
B. 11121314151617181920
C. 1112131415161718192021
D. infinite loop

View Answer


15. What will be the output of the following PHP code ?

<?php
$i=-5;
while($i){
        $i++;
        if($i%2==0)
                continue;
        else
        {
                $i++;
        }
       echo($i);
}
?>

A. 4 to 20
B. -20
C. Null
D. infinite loop

View Answer


16. What will be the output of the following PHP code ?

<?php
for ($i = 0; $i < 4; $i++)
{
  for ($j = 0; $j < 3; $j++)
  {
   if ($i > 1)
   continue;
   echo("Hi 
");
  }
}
?>

A. prints "Hi" 3 times
B. prints "Hi" 4 times
C. prints "Hi" 5 times
D. prints "Hi" 6 times

View Answer


17. What will be the output of the following PHP code ?

<?php
$i = 0;
$j = 0;
first: while ($i < 2)
{
 $i++;
  while ($j < 3)
    {
     $j++;               
      echo("loop
");
      if($j%2==0)
         continue;
      goto first;
    }
}
?>

A. prints "loop" infinite times
B. prints "loop" 1 times
C. prints "loop" 2 times
D. prints "loop" 3 times

View Answer


18. What will be the output of the following PHP code ?

<?php
for($i=0; $i<=5; $i++){
    $i = $i+1;
}

echo "$i";
?>

A. 5
B. 6
C. 7
D. 8

View Answer


19. What will be the output of the following PHP code ?

<?php
$a = 10;
$b = $a>2?"Hi":"Bye";
echo "$b";
?>

A. HiBye
B. Bye
C. Hi
D. Error

View Answer


20. What will be the output of the following PHP code ?

<?php
$a = 0;
if($a > 1 );
{
  echo "Sun";   
}
echo "Moon";
?>

A. SunMoon
B. Sun
C. Moon
D. Error

View Answer





Also check :


Discussion



* You must be logged in to add comment.