This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “Operators – 3”.
1. What will be the output of the following PHP code?
<?php
echo 5 * 9 / 3 + 9;
?>
a) 24
b) 3.7
c) 3.85
d) 0
View Answer
Explanation: Operator precedence order must be followed.
2. What will be the output of the following PHP code?
<?php
echo 5 * 9 / 3 + 9
?>
a) 24
b) 3.7
c) 3.85
d) 0
View Answer
Explanation: Operator precedence order must be followed.
3. What will be the output of the following PHP code?
<?php
$i = 0;
$j = 0;
if ($i && ($j = $i + 10)) {
echo "true";
}
echo $j;
?>
a) 10
b) 0
c) true0
d) true10
View Answer
Explanation: 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?
<?php
$i = 10;
$j = 0;
if ($i || ($j = $i + 10)) {
echo "true";
}
echo $j;
?>
a) 20
b) true0
c) 0
d) true20
View Answer
Explanation: 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?
<?php
$i = 1;
if ($i++ && ($i == 1))
printf("Yes\n$i");
else
printf("No\n$i");
?>
a) No 2
b) Yes 1
c) Yes 2
d) No 1
View Answer
Explanation: The first condition returns true and increments but the second condition is false.
6. What will be the output of the following PHP code?
<?php
$a = 1; $b = 3;
$d = $a++ + ++$b;
echo $d;
?>
a) 5
b) 4
c) 3
d) error
View Answer
Explanation: Post increment of a is done after expression evaluation.
7. What will be the output of the following PHP code?
<?php
$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
View Answer
Explanation: Follow the order of post and pre increments.
8. What will be the output of the following PHP code?
<?php
$a = 10; $b = 10;
if ($a = 5)
$b--;
print $a;print $b--;
?>
a) 58
b) 59
c) 109
d) 108
View Answer
Explanation: 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?
<?php
$i = 0;
$x = $i++; $y = ++$i;
print $x; print $y;
?>
a) 02
b) 12
c) 01
d) 21
View Answer
Explanation: First case i is incremented after setting x to i.
10. What will be the output of the following PHP code?
<?php
$a = 5; $b = -7; $c =0;
$d = ++$a && ++$b || ++$c;
print $d; print $a;
?>
a) 16
b) 06
c) 15
d) 05
View Answer
Explanation: 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?
<?php
$b = 1; $c = 4; $a = 5;
$d = $b + $c == $a;
print $d;
?>
a) 5
b) 0
c) 10
d) 1
View Answer
Explanation: First b and c are added and then tested if d=5, which is true thus return 1.
Sanfoundry Global Education & Learning Series – PHP Programming.
To practice all areas of PHP Programming, here is complete set of 1000+ Multiple Choice Questions and Answers on PHP.
- Check PHP Books
- Apply for Programming Internship
- Practice MCA MCQs
- Practice Programming MCQs
- Check Information Technology Books