PHP Coding Questions and Answers – Switch

This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “Switch”.

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

  1. <?php
  2. $a = "1";
  3. switch ($a)
  4. {
  5. case 1:
  6.     print "hi";
  7. case 2:
  8.     print "hello";
  9. default:
  10.     print "hi1";
  11. }
  12. ?>

a) hihellohi1
b) hi
c) hihi1
d) hi1
View Answer

Answer: a
Explanation: As break is not provided it executes all the cases.
advertisement
advertisement

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

  1. <?php
  2. $a = "2";
  3. switch ($a)
  4. {
  5. case 1:
  6.     print "hi";
  7. case 2:
  8.     print "hello";
  9.     break;
  10. default:
  11.     print "hi1";
  12. }
  13. ?>

a) hihellohi1
b) hello
c) hihi1
d) hi1
View Answer

Answer: b
Explanation: As hello is provided after case2 it breaks the loop.
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. $a = "1";
  3. switch($a)
  4. {
  5. case 1:
  6.     break;
  7.     print "hi";
  8. case 2:
  9.     print "hello";
  10.     break;
  11. default:
  12.     print "hi1";
  13. }
  14. ?>

a) hihellohi1
b) no output
c) hihi1
d) hi1
View Answer

Answer: b
Explanation: As break is provided before print statement in case 2 it breaks the loop before printing.
advertisement

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

  1. <?php
  2. $a = "1";
  3. $a = 1;
  4. $b = 1;
  5. switch($a)
  6. {
  7. case $a * $b: 
  8.     print "hi";
  9.     break;
  10. case $a / $b:
  11.     print "hello";
  12.     break;
  13. default:
  14.     print "hi1";
  15. }
  16. ?>

a) hihellohi1
b) hi
c) hihello
d) hi1
View Answer

Answer: b
Explanation: It checks the first case, when it finds it equal it will perform it breaks out.

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

  1. <?php
  2. $a = 97;
  3. switch($a)
  4. {
  5. case "a":
  6.     print "hi";
  7.     break;
  8. case 97:
  9.     print "hello";
  10.     break;
  11. default:
  12.     print "hi1";
  13. }
  14. ?>

a) hihellohi1
b) hi
c) hihello
d) hello
View Answer

Answer: d
Explanation: Downcasting does not happen in case, it compares only with its data type.

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

  1. <?php
  2. $b = 1;
  3. switch($b)
  4. {
  5. case 1.0:
  6.     print "hi";
  7.     break;
  8. case 1:
  9.     print "hello";
  10.     break;
  11. default:
  12.     print "hi1";
  13. }
  14. ?>

a) hihellohi1
b) hi
c) hihello
d) hello
View Answer

Answer: a
Explanation: Upcasting does happen in case, it compares it with 1.0 and thus prints hi and exits.

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

  1. <?php
  2. const $b = 1;
  3. switch($b)
  4. {
  5. case 1:
  6.     print "hi";
  7.     break;
  8. case 1:
  9.     print "hello";
  10.     break;
  11. default:
  12.     print "hi1";
  13. }
  14. ?>

a) error
b) hi
c) hihello
d) hello
View Answer

Answer: a
Explanation: Constants cannot be used in switch cases.

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

  1. <?php
  2. $b = 1;
  3. switch(print $b)
  4. {
  5. case 2:
  6.     print "hello";
  7.     break;
  8. case 1:
  9.     print "hi";
  10.     break;
  11. default:
  12.     print "hi1";
  13. }
  14. ?>

a) 1hello
b) 1hi
c) 1hi1
d) error
View Answer

Answer: b
Explanation: Print returns 1, thus it gives case 1.

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

  1. <?php
  2. switch($b)
  3. {
  4. case 2:
  5.     print "hello";
  6.     break;
  7. case 1:
  8.     print "hi";
  9.     break;
  10. }
  11. ?>

a) hello
b) hi
c) no output
d) error
View Answer

Answer: c
Explanation: If that case does not exist then it searches for default and on not finding it does nothing.

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

  1. <?php
  2. switch($b)
  3. {
  4. case 2:
  5.     print "hello";
  6.     break;
  7. case b:
  8.     print "hi";
  9.     break;
  10. }
  11. ?>

a) hello
b) hi
c) no output
d) error
View Answer

Answer: c
Explanation: Case cannot be defined by a variable.

Sanfoundry Global Education & Learning Series – PHP Programming.

To practice all areas of PHP for Campus Interviews, 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.