250+ TOP PHP Coding MCQs on While Loops and Answers

Tricky PHP Interview Questions & Answers on “While Loops – 2”.

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

  1. $i = 0;
  2. while($i = 10)
  3. {   
  4.     print "hi";
  5. }
  6. print "hello";
  7. ?>

a) hello
b) infinite loop
c) hihello
d) error
Answer: b
Clarification: While condition always gives 1.

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

  1. $i = "";
  2. while ($i = 10)
  3. {   
  4.     print "hi";
  5. }
  6. print "hello";
  7. ?>

a) hello
b) infinite loop
c) hihello
d) error
Answer: b
Clarification: While condition always gives 1.

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

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

a) 4hello4hello4hello4hello4hello…..infinite
b) 5hello5hello5hello5hello5hello…..infinite
c) no output
d) error
Answer: b
Clarification: i is decremented in the while loop in the condition check and then incremented back.

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

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

a) 5
b) 555555555…infinitely
c) 54321
d) error
Answer: b
Clarification: As it is && operator it is being incremented and decremented continuously.

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

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

a) 54321111111….infinitely
b) 555555555…infinitely
c) 54321
d) 5
Answer: a
Clarification: As it is || operator the second expression is not evaluated till i becomes 1 then it goes into a loop.

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

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

a) 1234567891011121314….infinitely
b) 01234567891011121314…infinitely
c) 1
d) 0

Answer: a
Clarification: As it is || operator the second expression is not evaluated and i is always incremented, in the first case to 1.

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

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

a) 1234567891011121314….infinitely
b) 01234567891011121314…infinitely
c) no output
d) error
Answer: c
Clarification: The first condition itself fails thus the loop exists.

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

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

a) 00000000000000000000….infinitely
b) -1-1-1-1-1-1-1-1-1-1…infinitely
c) no output
d) error
Answer: a
Clarification: (–$i > ++$i) evaluates to 0 but -1 makes it enters the loop and prints i.

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

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

a) 210
b) 10
c) no output
d) infinite loop
Answer: a
Clarification: The loop ends when i becomes 0.

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

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

a) 210
b) 10
c) no output
d) infinite loop
Answer: d
Clarification: The loop never ends as i is always incremented and then decremented.

To practice all tricky interview questions on PHP,

Leave a Comment

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

Scroll to Top