250+ TOP PHP Coding MCQs on For Loops and Answers

PHP online quiz on “For Loops – 3”.

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

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

a) infinite loop
b) 0
c) no output
d) error
Answer: c
Clarification: The condition of the loop is always false 0.

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

  1. for ($i = 0; $i < 5; $i++) 
  2. {
  3.     for ($j = $i; $j > 0; $i--)
  4.         print $i;
  5. }
  6. ?>

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
Answer: a
Clarification: In the second loop j value is not being changed.

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

  1. for ($i = 0; $i < 5; $i++)  
  2. {
  3.     for ($j = $i;$j > $i; $i--)
  4.         print $i;
  5. }
  6. ?>

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
Answer: d
Clarification: The second loop does not execute as the check condition is always false.

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

  1. $user = array("Ashley", "Bale", "Shrek", "Blank");
  2. for ($x = 0; $x < count($user); $x++) 
  3. {
  4.     if ($user[$x] == "Shrek") 
  5. 	    continue;
  6.     printf ($user[$x]); 
  7. }
  8. ?>

a) AshleyBaleBlank
b) AshleyBale
c) AshleyBaleShrek
d) No output
Answer: a
Clarification: Only the Shrek is skipped due to the continue statement.

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

  1. $user = array("Ashley", "Bale", "Shrek", "Blank");
  2.     for ($x=0; $x < count($user) - 1; $x++)	
  3.     {
  4.         if ($user[$x++] == "Shrek") 
  5. 		    continue;
  6.         printf ($user[$x]); 
  7.     }
  8. ?>

a) AshleyBaleBlank
b) Bale
c) AshleyShrek
d) BaleBlank

Answer: a
Clarification: Only Bale is printed as $x++ is done before printing and then checked.

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

  1. $user = array("Ashley", "Bale", "Shrek", "Blank");
  2. for ($x = 0; $x < count($user); $x) 
  3. {
  4.     if ($user[$x++] == "Shrek") 
  5. 	    continue;
  6.     printf ($user[$x]); 
  7. }
  8. ?>

a) AshleyBaleBlank
b) BaleShrek
c) AshleyBlank
d) Bale
Answer: b
Clarification: x is incremented only inside loop i the if condition.

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

  1. for ($i = 0; $i % ++$i; $i++) 
  2. {
  3.     print"i";
  4. }
  5. ?>

a) error
b) infinite loop
c) no output
d) 0
Answer: b
Clarification: 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?

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

a) iiiii
b) infinite loop
c) iiiiiiiiiiiiiiiiiiiiiiiii
d) no output
Answer: a
Clarification: 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?

  1. $a = array("hi", "hello", "bye");
  2. foreach ($a as $value) 
  3. {
  4.     if (count($a) == 2)
  5. 	    print $value;         
  6. }
  7. ?>

a) hihellobye
b) infinite loop
c) hihello
d) no output
Answer: d
Clarification: As count($a) returns 3 the condition is always false.

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

  1. $a = array("hi", "hello", "bye");
  2. for (;count($a) < 5;) 
  3. {
  4.     if (count($a) == 3)
  5. 	    print $a;         
  6. }
  7. ?>

a) ArrayArrayArrayArrayArrayArray….infinitely
b) (“hi”,”hello”,”bye”)(“hi”,”hello”,”bye”)(“hi”,”hello”,”bye”)(“hi”,”hello”,”bye”)…infinitely
c) hihellobyehihellobyehihellobyehihellobyehihellobyehihellobye…..infinitely
d) no output

Answer: a
Clarification: As count($a) returns 3 the condition is always true, thus it prints $a, which returns its data type.

Leave a Comment

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

Scroll to Top