PHP Multiple Choice Questions on “Switch”.
1. What will be the output of the following PHP code?
-
-
$a = "1";
-
switch ($a)
-
{ -
case 1:
-
print "hi";
-
case 2:
-
print "hello";
-
default:
-
print "hi1";
-
} -
?>
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?
-
-
$a = "2";
-
switch ($a)
-
{ -
case 1:
-
print "hi";
-
case 2:
-
print "hello";
-
break;
-
default:
-
print "hi1";
-
} -
?>
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?
-
-
$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
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?
-
-
$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
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?
-
-
$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
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?
-
-
$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
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?
-
-
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
Answer: a
Clarification: Constants cannot be used in switch cases.
8. What will be the output of the following PHP code?
-
-
$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
Answer: b
Clarification: Print returns 1, thus it gives case 1.
9. What will be the output of the following PHP code?
-
-
switch($b)
-
{ -
case 2:
-
print "hello";
-
break;
-
case 1:
-
print "hi";
-
break;
-
} -
?>
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?
-
-
switch($b)
-
{ -
case 2:
-
print "hello";
-
break;
-
case b:
-
print "hi";
-
break;
-
} -
?>
a) hello
b) hi
c) no output
d) error
