PHP Coding Questions and Answers – For Loops – 2

This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “For Loops – 2”.

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

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

a) Hello….36 times
b) Hello….45 times
c) Hello….50 times
d) Hello….40 times
View Answer

Answer: a
Explanation: 9*4 times is printed.
advertisement
advertisement

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

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

a) Infinite
b) 123…….20
c) 1357…19
d) 13579…21
View Answer

Answer: a
Explanation: Condition always fails as count takes only odd numbers.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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

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

a) 20
b) 19
c) 12345678910….19
d) 12345678910….1920
View Answer

Answer: a
Explanation: The for loop has no body, it just runs till condition is satisfied.
advertisement

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

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

a) hihihi
b) hihellohihellohihello
c) hellohellohello
d) hi
View Answer

Answer: a
Explanation: When continue is encountered it skips to the next iteration.

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

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

a) hihihi
b) hihellohihellohihello
c) hellohellohello
d) hi
View Answer

Answer: d
Explanation: When break is encountered it leaves the loop.

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

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

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

Answer: a
Explanation: 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. <?php
  2. for ($i = 0;$i = -1;$i = 1)
  3. {
  4.     print $i;
  5.     if ($i != 1) 
  6. 	break;
  7. }
  8. ?>

a) 0
b) infinite loop
c) -1
d) 1
View Answer

Answer: c
Explanation: 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. <?php
  2. for(;;)
  3. {
  4.    print "10";
  5. }
  6. ?>

a) 10
b) infinite loop
c) no output
d) error
View Answer

Answer: b
Explanation: There is no check condition to stop the execution of the loop.

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

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

a)

   
     *
    **
   ***

b)

   ***
   **
   * 

c)

   *
   **
   ***  

d) error
View Answer

Answer: a
Explanation: 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. <?php
  2. for ($i = 0; -5 ; $i++)
  3. {
  4.     print"i";
  5.     if ($i == 3)
  6.         break;
  7. }
  8. ?>

a) 0 1 2 3 4
b) 0 1 2 3
c) 0 1 2 3 4 5
d) error
View Answer

Answer: b
Explanation: The break statement after breaks the loop after i=3, does not print anymore.

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.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.