PHP Questions and Answers for Aptitude test on “Functions – 4”.
1. What will be the output of the following PHP code?
-
-
function A1($x)
-
{ -
switch($x)
-
{ -
case 1:
-
//this statement is the same as if($x == 1) -
echo 'Case 1 was executed.';
-
break;
-
case 2:
-
//this statement is the same as if($x == 2) -
echo 'Case 2 was executed.';
-
break;
-
case 3:
-
//this statement is the same as if($x == 3) -
echo 'Case 3 was executed.';
-
break;
-
case 4:
-
//this statement is the same as if($x == 4) -
echo 'Case 4 was executed.';
-
break;
-
default:
-
//this statement is the same as if $x does not equal the other conditions -
echo 'Default was executed.';
-
break;
-
-
} -
} -
A1(9);
-
?>
a) Case 1 was executed
b) Case 2 was executed
c) Default was executed
d) Case 4 was executed
Answer: d
Clarification: The switch statement is executed with $x = 9.
2. What will be the output of the following PHP code?
-
-
function uppercase($string)
-
{ -
echo ucwords($string);
-
} -
$wow = "uppercase";
-
$wow("Time to live king size");
-
?>
a) TIME TO LIVE KING SIZE
b) Time to live king size
c) Uppercase
d) Time To Live King Size
Answer: d
Clarification: The ucwords() function converts the first character of each word in a string to uppercase.
3. What will be the output of the following PHP code?
-
-
function TV($string)
-
{ -
echo "my favourite TV show is ".$string;
-
function b()
-
{ -
echo " I am here to spoil this code";
-
} -
} -
b();
-
?>
a) I am here to spoil this code
b) Error
c) My favourite TV show isI am here to spoil this code
d) None of the mentioned
Answer: b
Clarification: b is undeclared if TV() is not called first.
4. What will be the output of the following PHP code?
-
-
function TV($string)
-
{ -
echo "my favourite TV show is ".$string;
-
function b()
-
{ -
echo " I am here to spoil this code";
-
} -
} -
function b()
-
{ -
echo " I am here to spoil this code";
-
} -
b();
-
?>
a) I am here to spoil this code
b) Error
c) my favourite TV show isI am here to spoil this code
d) None of the mentioned
Answer: a
Clarification: This one works because b is declared independent of TV() also.
5. What will be the output of the following PHP code?
-
-
function TV($string)
-
{ -
echo "my favourite TV show is ".$string;
-
function b()
-
{ -
echo " I am here to spoil this code";
-
} -
} -
function b()
-
{ -
echo " I am here to spoil this code";
-
} -
b();
-
TV("Sherlock");
-
?>
a) I am here to spoil this code
b) Error
c) My favourite TV show isI am here to spoil this code
d) None of the mentioned
Answer: b
Clarification: Function b is declared twice.
6. What will be the output of the following PHP code?
-
-
function TV($string)
-
{ -
echo "my favourite TV show is ".$string;
-
function b()
-
{ -
echo " I am here to spoil this code";
-
} -
} -
a("Sherlock");
-
b();
-
?>
a) I am here to spoil this code
b) Error
c) my favourite TV show is SherlockI am here to spoil this code
d) None of the mentioned
Answer: c
Clarification: b is declared as TV() is executed first.
7. What will be the output of the following PHP code?
-
-
function calc($num1, $num2)
-
{ -
$total = $num1 * $num2;
-
} -
$result = calc(42, 0);
-
echo $result;
-
?>
a) Error
b) 0
c) 42
d) 84
