PHP Questions and Answers for Entrance exams on “Operators – 3”.
1. What will be the output of the following PHP code?
-
-
echo 5 * 9 / 3 + 9;
-
?>
a) 24
b) 3.7
c) 3.85
d) 0
Answer: a
Clarification: Operator precedence order must be followed.
2. What will be the output of the following PHP code?
-
-
echo 5 * 9 / 3 + 9
-
?>
a) 24
b) 3.7
c) 3.85
d) 0
Answer: a
Clarification: Operator precedence order must be followed.
3. What will be the output of the following PHP code?
-
-
$i = 0;
-
$j = 0;
-
if ($i && ($j = $i + 10)) {
-
echo "true";
-
} -
echo $j;
-
?>
a) 10
b) 0
c) true0
d) true10
Answer: b
Clarification: In if condition when the first case is 0 and is an && operation then the second command is not executed.
4. What will be the output of the following PHP code?
-
-
$i = 10;
-
$j = 0;
-
if ($i || ($j = $i + 10)) {
-
echo "true";
-
} -
echo $j;
-
?>
a) 20
b) true0
c) 0
d) true20
Answer: b
Clarification: In if condition when the first case is 1 and is an || operation then the second command is not executed.
5. What will be the output of the following PHP code?
-
-
$i = 1;
-
if ($i++ && ($i == 1))
-
printf("Yesn$i");
-
else -
printf("Non$i");
-
?>
a) No 2
b) Yes 1
c) Yes 2
d) No 1
Answer: a
Clarification: The first condition returns true and increments but the second condition is false.
6. What will be the output of the following PHP code?
-
-
$a = 1; $b = 3;
-
$d = $a++ + ++$b;
-
echo $d;
-
?>
a) 5
b) 4
c) 3
d) error
Answer: a
Clarification: Post increment of a is done after expression evaluation.
7. What will be the output of the following PHP code?
-
-
$a = 1; $b = 1; $d = 1;
-
print ++$a + ++$a+$a++; print $a++ + ++$b; print ++$d + $d++ + $a++;
-
?>
a) 869
b) 742
c) 368
d) error
Answer: a
Clarification: Follow the order of post and pre increments.
8. What will be the output of the following PHP code?
-
-
$a = 10; $b = 10;
-
if ($a = 5)
-
$b--;
-
print $a;print $b--;
-
?>
a) 58
b) 59
c) 109
d) 108
Answer: b
Clarification: a is set to 5 in the if condition and b is post decremented in the print statement.
9. What will be the output of the following PHP code?
-
-
$i = 0;
-
$x = $i++; $y = ++$i;
-
print $x; print $y;
-
?>
a) 02
b) 12
c) 01
d) 21
Answer: a
Clarification: First case i is incremented after setting x to i.
10. What will be the output of the following PHP code?
-
-
$a = 5; $b = -7; $c =0;
-
$d = ++$a && ++$b || ++$c;
-
print $d; print $a;
-
?>
a) 16
b) 06
c) 15
d) 05
Answer: a
Clarification: 1&&0||1 is evaluated to 1 and the a is also preincremented to 6.
11. What will be the output of the following PHP code?
-
-
$b = 1; $c = 4; $a = 5;
-
$d = $b + $c == $a;
-
print $d;
-
?>
a) 5
b) 0
c) 10
d) 1
Answer: d
Clarification: First b and c are added and then tested if d=5, which is true thus return 1.
PHP for Entrance exams,
