This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “Switch”.
1. What will be the output of the following PHP code?
<?php
$a = "1";
switch ($a)
{
case 1:
print "hi";
case 2:
print "hello";
default:
print "hi1";
}
?>
a) hihellohi1
b) hi
c) hihi1
d) hi1
View Answer
Explanation: As break is not provided it executes all the cases.
2. What will be the output of the following PHP code?
<?php
$a = "2";
switch ($a)
{
case 1:
print "hi";
case 2:
print "hello";
break;
default:
print "hi1";
}
?>
a) hihellohi1
b) hello
c) hihi1
d) hi1
View Answer
Explanation: As hello is provided after case2 it breaks the loop.
3. What will be the output of the following PHP code?
<?php
$a = "1";
switch($a)
{
case 1:
break;
print "hi";
case 2:
print "hello";
break;
default:
print "hi1";
}
?>
a) hihellohi1
b) no output
c) hihi1
d) hi1
View Answer
Explanation: 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?
<?php
$a = "1";
$a = 1;
$b = 1;
switch($a)
{
case $a * $b:
print "hi";
break;
case $a / $b:
print "hello";
break;
default:
print "hi1";
}
?>
a) hihellohi1
b) hi
c) hihello
d) hi1
View Answer
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?
<?php
$a = 97;
switch($a)
{
case "a":
print "hi";
break;
case 97:
print "hello";
break;
default:
print "hi1";
}
?>
a) hihellohi1
b) hi
c) hihello
d) hello
View Answer
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?
<?php
$b = 1;
switch($b)
{
case 1.0:
print "hi";
break;
case 1:
print "hello";
break;
default:
print "hi1";
}
?>
a) hihellohi1
b) hi
c) hihello
d) hello
View Answer
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?
<?php
const $b = 1;
switch($b)
{
case 1:
print "hi";
break;
case 1:
print "hello";
break;
default:
print "hi1";
}
?>
a) error
b) hi
c) hihello
d) hello
View Answer
Explanation: Constants cannot be used in switch cases.
8. What will be the output of the following PHP code?
<?php
$b = 1;
switch(print $b)
{
case 2:
print "hello";
break;
case 1:
print "hi";
break;
default:
print "hi1";
}
?>
a) 1hello
b) 1hi
c) 1hi1
d) error
View Answer
Explanation: Print returns 1, thus it gives case 1.
9. What will be the output of the following PHP code?
<?php
switch($b)
{
case 2:
print "hello";
break;
case 1:
print "hi";
break;
}
?>
a) hello
b) hi
c) no output
d) error
View Answer
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?
<?php
switch($b)
{
case 2:
print "hello";
break;
case b:
print "hi";
break;
}
?>
a) hello
b) hi
c) no output
d) error
View Answer
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.
- Check MCA Books
- Apply for Programming Internship
- Check Information Technology Books
- Practice MCA MCQs
- Check PHP Books