ExamVeda
Login
Home
31
What will be the output of the following PHP code ?
<?php
$a = "hello";
if (strlen($a))
    print strlen($a);
else
    print "hi";
?>
Discuss
Answer & Solution
Answer: Option B
Solution:
The function strlen($a) gives the length of the string,5, which is considered true.
32
What will be the output of the following PHP code ?
<?php
$a = "1";
$b = "0";
if ((int)$a && $b) 
    print"hi";
else 
    print "hello";
?>
Discuss
Answer & Solution
Answer: Option A
Solution:
The expression is evaluated with values contained in the string, even without typecasting.
33
What will be the output of the following PHP code ?
<?php
$a = "1";
switch ($a)
{
case 1:
    print "hi";
case 2:
    print "hello";
default:
    print "hi1";
}
?>
Discuss
Answer & Solution
Answer: Option A
Solution:
As break is not provided it executes all the cases.
34
What will be the output of the following PHP code ?
<?php
$a = "2";
switch ($a)
{
case 1:
    print "hi";
case 2:
    print "hello";
    break;
default:
    print "hi1";
}
?>
Discuss
Answer & Solution
Answer: Option B
Solution:
As hello is provided after case2 it breaks the loop.
35
What will be the output of the following PHP code ?
<?php
$a = "1";
switch($a)
{
case 1:
    break;
    print "hi";
case 2:
    print "hello";
    break;
default:
    print "hi1";
}
?>
Discuss
Answer & Solution
Answer: Option B
Solution:
As break is provided before print statement in case 2 it breaks the loop before printing.
36
What will be the output of the following PHP code ?
<?php
$a = "1";
$a = 1;
$b = 1;
switch($a)
{
case $a * $b: 
    print "hi";
    break;
case $a / $b:
    print "hello";
    break;
default:
    print "hi1";
}
?>
Discuss
Answer & Solution
Answer: Option B
Solution:
It checks the first case, when it finds it equal it will perform it breaks out.
37
What will be the output of the following PHP code ?
<?php
$a = 97;
switch(?$a)
{
case "a":
    print "hi";
    break;
case 97:
    print "hello";
    break;
default:
    print "hi1";
}
?>
Discuss
Answer & Solution
Answer: Option D
Solution:
Downcasting does not happen in case,it compares only with its data type.
38
What will be the output of the following PHP code ?
<?php
$b = 1;
switch($b)
{
case 1.0:
    print "hi";
    break;
case 1:
    print "hello";
    break;
default:
    print "hi1";
}
?>
Discuss
Answer & Solution
Answer: Option A
Solution:
Upcasting does happen in case,it compares it with 1.0 and thus prints hi and exits.
39
What will be the output of the following PHP code ?
<?php
const $b = 1;
switch($b)
{
case 1:
    print "hi";
    break;
case 1:
    print "hello";
    break;
default:
    print "hi1";
}
?>
Discuss
Answer & Solution
Answer: Option A
Solution:
Constants cannot be used in switch cases.
40
What will be the output of the following PHP code ?
<?php
$b = 1;
switch(print $b)
{
case 2:
    print "hello";
    break;
case 1:
    print "hi";
    break;
default:
    print "hi1";
}
?>
Discuss
Answer & Solution
Answer: Option B
Solution:
Print returns 1,thus it gives case 1.