250+ TOP PHP Coding MCQs on Switch and Answers

PHP Multiple Choice Questions on “Switch”.

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

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

a) hihellohi1
b) hi
c) hihi1
d) hi1
Answer: a
Clarification: As break is not provided it executes all the cases.

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

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

a) hihellohi1
b) hello
c) hihi1
d) hi1
Answer: b
Clarification: As hello is provided after case2 it breaks the loop.

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

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

a) hihellohi1
b) no output
c) hihi1
d) hi1
Answer: b
Clarification: As break is provided before print statement in case 2 it breaks the loop before printing.

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

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

a) hihellohi1
b) hi
c) hihello
d) hi1
Answer: b
Clarification: 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. $a = 97;
  2. switch($a)
  3. {
  4. case "a":
  5.     print "hi";
  6.     break;
  7. case 97:
  8.     print "hello";
  9.     break;
  10. default:
  11.     print "hi1";
  12. }
  13. ?>

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

Answer: d
Clarification: 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. $b = 1;
  2. switch($b)
  3. {
  4. case 1.0:
  5.     print "hi";
  6.     break;
  7. case 1:
  8.     print "hello";
  9.     break;
  10. default:
  11.     print "hi1";
  12. }
  13. ?>

a) hihellohi1
b) hi
c) hihello
d) hello
Answer: a
Clarification: 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. const $b = 1;
  2. switch($b)
  3. {
  4. case 1:
  5.     print "hi";
  6.     break;
  7. case 1:
  8.     print "hello";
  9.     break;
  10. default:
  11.     print "hi1";
  12. }
  13. ?>

a) error
b) hi
c) hihello
d) hello
Answer: a
Clarification: Constants cannot be used in switch cases.

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

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

a) 1hello
b) 1hi
c) 1hi1
d) error
Answer: b
Clarification: Print returns 1, thus it gives case 1.

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

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

a) hello
b) hi
c) no output
d) error
Answer: c
Clarification: 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. switch($b)
  2. {
  3. case 2:
  4.     print "hello";
  5.     break;
  6. case b:
  7.     print "hi";
  8.     break;
  9. }
  10. ?>

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

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

PHP for Campus Interviews,

Leave a Comment

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

Scroll to Top