PHP Question Bank on ” If-Else-If-2″.
1. What will be the output of the following PHP code?
-
-
$x = 10;
-
$y = 20;
-
if ($x > $y + $y != 3)
-
print "hi" ;
-
else -
print "how are u";
-
?>
a) how are u
b) hi
c) error
d) no output
Answer: b
Clarification: Expression evaluates to true.
2. What will be the output of the following PHP code?
-
-
$x = 10;
-
$y = 20;
-
if ($x > $y && 1||1)
-
print "hi" ;
-
else -
print "how are u";
-
?>
a) how are u
b) hi
c) error
d) no output
Answer: b
Clarification: Expression evaluates to true.
3. What will be the output of the following PHP code?
-
-
$x = 10;
-
$y = 20;
-
if ($x > $y && 1||1)
-
print "hi" ;
-
else -
print "how are u";
-
?>
a) how are u
b) hi
c) error
d) no output
Answer: b
Clarification: Expression evaluates to true.
4. What will be the output of the following PHP code?
-
-
if (-100)
-
print "hi" ;
-
else -
print "how are u";
-
?>
a) how are u
b) hi
c) error
d) no output
Answer: b
Clarification: Expression evaluates to true.
5. What will be the output of the following PHP code?
-
-
if (0.1)
-
print "hi" ;
-
else -
print "how are u";
-
?>
a) how are u
b) hi
c) error
d) no output
Answer: b
Clarification: Expression evaluates to true.
6. What will be the output of the following PHP code?
-
-
if (0.0)
-
print "hi" ;
-
else -
print "how are u";
-
?>
a) how are u
b) hi
c) error
d) no output
Answer: a
Clarification: Expression evaluates to false.
7. What will be the output of the following PHP code?
-
-
if (print "0")
-
print "hi" ;
-
else -
print "how are u";
-
?>
a) 0how are u
b) 0hi
c) hi
d) how are u
Answer: b
Clarification: Expression evaluates to true as print returns 1.
8. What will be the output of the following PHP code?
-
-
$x = 1;
-
if ($x == 2)
-
print "hi" ;
-
else if($x = 2)
-
print $x;
-
else -
print "how are u";
-
?>
a) error
b) 2
c) hi
d) how are u
Answer: b
Clarification: Enters if else as first condition is false and thus x is set to 2.
9. What will be the output of the following PHP code?
-
-
$x = 1;
-
if ($x = $x&0)
-
print $x ;
-
else -
print "how are u";
-
?>
a) 0
b) 1
c) error
d) how are u
Answer: d
Clarification: x&0 is 0,thus evaluated to false.
10. What will be the output of the following PHP code?
-
-
$x = 1;
-
if ($x = $x&0)
-
print $x ;
-
else -
print "how are u";
-
?>
a) 0
b) 1
c) error
d) how are u
Answer: d
Clarification: x&0 is 0,thus evaluated to false.
11. What will be the output of the following PHP code?
-
-
$x = 1;
-
if ($x = $x&0)
-
print $x;
-
else -
break;
-
?>
a) 0
b) 1
c) error
d) no output
Answer: c
Clarification: break is not defined for a if else ladder.
12. What will be the output of the following PHP code?
-
-
$a = 100;
-
if ($a > 10)
-
printf("M.S. Dhoni");
-
else if ($a > 20)
-
printf("M.E.K Hussey");
-
else if($a > 30)
-
printf("A.B. de villiers");
-
?>
a) M.S.Dhoni
b) M.E.K.Hussey
c)
M.S.Dhoni M.E.K.Hussey A.B.de villiers
d) No output
