250+ TOP PHP Coding MCQs on For Loops and Answers

PHP Multiple Choice Questions on “For Loops – 2”.

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

  1. for ($x = 1; $x < 10; $x++)
  2.     for ($y = 1; $y < 5; $y++)
  3.         print "Hello";
  4. ?>

a) Hello….36 times
b) Hello….45 times
c) Hello….50 times
d) Hello….40 times
Answer: a
Clarification: 9*4 times is printed.

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

  1. for ($count = 1; $count != 20;$count++)
  2. {
  3.     print $count;
  4.     $count++;
  5. }
  6. ?>

a) Infinite
b) 123…….20
c) 1357…19
d) 13579…21
Answer: a
Clarification: Condition always fails as count takes only odd numbers.

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

  1. for ($count = 1; $count < 20; $count++);
  2.     print $count;
  3. ?>

a) 20
b) 19
c) 12345678910….19
d) 12345678910….1920
Answer: a
Clarification: The for loop has no body, it just runs till condition is satisfied.

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

  1. for ($count = 0; $count < 3;$count++);
  2. {
  3.     print "hi";continue;print "hello";
  4. }
  5. ?>

a) hihihi
b) hihellohihellohihello
c) hellohellohello
d) hi
Answer: a
Clarification: When continue is encountered it skips to the next iteration.

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

  1. for ($count = 0; $count<3;$count++);
  2. {
  3.     print "hi";break;print "hello";
  4. }
  5. ?>

a) hihihi
b) hihellohihellohihello
c) hellohellohello
d) hi
Answer: d
Clarification: When break is encountered it leaves the loop.

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

  1. for(++$i; ++$i; ++$i)
  2. {
  3.     print $i;
  4.     if ($i == 4) 
  5.         break;
  6. }
  7. ?>

a) 24
b) 134
c) 1234
d) 1

Answer: a
Clarification: The order of execution is initialization, check, increment/decrement, check, increment/decrement, check, increment/decrement….so on.

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

  1. for ($i = 0;$i = -1;$i = 1)
  2. {
  3.     print $i;
  4.     if ($i != 1) 
  5. 	break;
  6. }
  7. ?>

a) 0
b) infinite loop
c) -1
d) 1
Answer: c
Clarification: The order of execution is initialization, check, increment/decrement, check, increment/decrement, check, increment/decrement….so on.

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

  1. for(;;)
  2. {
  3.    print "10";
  4. }
  5. ?>

a) 10
b) infinite loop
c) no output
d) error
Answer: b
Clarification: There is no check condition to stop the execution of the loop.

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

  1. for ($i = 0; $i < 3; $i++)
  2. {
  3.     for($j = $i; $j > 0; $j--)
  4.         print " ";
  5.     for($k = $j; $k < 3; $k++)
  6.         print "*";
  7. 	print "n";  
  8. }
  9. ?>

a)

   
     *
    **
   ***

b)

   ***
   **
   * 

c)

   *
   **
   ***  

d) error
Answer: a
Clarification: Follow the trace of i, j prints 3 – i no of spaces for each i, k prints i stars for each loop.

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

  1. for ($i = 0; -5 ; $i++)
  2. {
  3.     print"i";
  4.     if ($i == 3)
  5.         break;
  6. }
  7. ?>

a) 0 1 2 3 4
b) 0 1 2 3
c) 0 1 2 3 4 5
d) error
Answer: b
Clarification: The break statement after breaks the loop after i=3, does not print anymore.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top