ExamVeda
Login
Home
1
What will be the output of the following PHP code?
<?php
$x;
if ($x)
    print "hi" ;
else
    print "how are u";
?>
Discuss
Answer & Solution
Answer: Option A
Solution:
Uninitialized x is set to 0, thus if condition fails.
2
What will be the output of the following PHP code ?
<?php
$x = 0;
if ($x++)
    print "hi";
else
    print "how are u";
?>
Discuss
Answer & Solution
Answer: Option D
Solution:
x is incremented after if which evaluates to false.
3
What will be the output of the following PHP code ?
<?php
$x;
if ($x == 0)
    print "hi" ;
else
    print "how are u";
    print "hello"
?>
Discuss
Answer & Solution
Answer: Option B
Solution:
else condition without brackets performs the following statements only.
4
What will be the output of the following PHP code ?
<?php
$x = 0;
if ($x == 1)
    if ($x >= 0)
	print "true";
    else
	print "false"; 
?>
Discuss
Answer & Solution
Answer: Option D
Solution:
The nested for loop is not entered if outer condition is false.
5
What will be the output of the following PHP code ?
<?php
$a = 1;
if ($a--)
    print "True";
if ($a++)
    print "False"; 
?>
Discuss
Answer & Solution
Answer: Option A
Solution:
Due to post increment and post decrement only the first condition is satisfied.
6
What will be the output of the following PHP code ?
<?php
$a = 1;
if (echo $a)
    print "True";
else
    print "False"; 
?>
Discuss
Answer & Solution
Answer: Option C
Solution:
echo does not return anything so if condition is empty.
7
What will be the output of the following PHP code ?
<?php
$a = 1;
if (print $a)
    print "True";
else
    print "False"; 
?>
Discuss
Answer & Solution
Answer: Option A
Solution:
print returns 1 if it prints anything.
8
What will be the output of the following PHP code ?
<?php
$a = 10;
if (1) 
    print "all";
else 
    print "some"
else 
    print "none";
?>
Discuss
Answer & Solution
Answer: Option C
Solution:
Hanging else statement.
9
What will be the output of the following PHP code ?
<?php
$a = 10;
if (0) 
    print "all";
 if 
 else 
     print "some"
?>
Discuss
Answer & Solution
Answer: Option C
Solution:
No else statement to end the if statement.
10
What will be the output of the following PHP code ?
<?php
$a = "";
if ($a) 
    print "all";
if 
else 
    print "some";
?>
Discuss
Answer & Solution
Answer: Option B
Solution:
Empty string is evaluated to 0.