250+ TOP PHP Coding MCQs on Functions and Answers

PHP Questions and Answers for Aptitude test on “Functions – 4”.

1. What will be the output of the following PHP code?

  1.     function A1($x)
  2.     {
  3.         switch($x)
  4.         {
  5.         case 1: 
  6.             //this statement is the same as if($x == 1)
  7.             echo 'Case 1 was executed.';
  8.             break;
  9.         case 2: 
  10.             //this statement is the same as if($x == 2)
  11.             echo 'Case 2 was executed.';
  12.             break;
  13.         case 3: 
  14.             //this statement is the same as if($x == 3)
  15.             echo 'Case 3 was executed.';
  16.             break;
  17.         case 4: 
  18.             //this statement is the same as if($x == 4)
  19.             echo 'Case 4 was executed.';
  20.             break;
  21.         default: 
  22.             //this statement is the same as if $x does not equal the other conditions
  23.             echo 'Default was executed.';
  24.             break;
  25.  
  26.         }
  27.     }
  28.     A1(9);
  29. ?>

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?

  1.     function uppercase($string)
  2.     {
  3.         echo ucwords($string);
  4.     }
  5.     $wow = "uppercase";
  6.     $wow("Time to live king size");
  7. ?>

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?

  1.     function TV($string)
  2.     {
  3.         echo "my favourite TV show is ".$string;
  4.         function b()
  5.         {
  6.             echo " I am here to spoil this code";
  7.         }
  8.     }
  9.     b();
  10. ?>

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?

  1.     function TV($string)
  2.     {
  3.         echo "my favourite TV show is ".$string;
  4.         function b()
  5.         {
  6.             echo " I am here to spoil this code";
  7.         }
  8.     }
  9.     function b()
  10.     {
  11.         echo " I am here to spoil this code";
  12.     }
  13.     b();
  14. ?>

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?

  1.     function TV($string)
  2.     {
  3.         echo "my favourite TV show is ".$string;
  4.         function b()
  5.         {
  6.             echo " I am here to spoil this code";
  7.         }
  8.     }
  9.     function b()
  10.     {
  11.         echo " I am here to spoil this code";
  12.     }
  13.     b();
  14.     TV("Sherlock");
  15. ?>

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?

  1.     function TV($string)
  2.     {
  3.         echo "my favourite TV show is ".$string;
  4.         function b()
  5.         {
  6.             echo " I am here to spoil this code";
  7.         }
  8.     }
  9.     a("Sherlock");
  10.     b();
  11. ?>

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?

  1.     function calc($num1, $num2)
  2.     {
  3.         $total = $num1 * $num2; 
  4.     }
  5.     $result = calc(42, 0);
  6.     echo $result;    
  7. ?>

a) Error
b) 0
c) 42
d) 84

Answer: a
Clarification: Function does not return anything.

8. What will be the output of the following PHP code?

  1.     function calc($num1, $num2)
  2.     {
  3.         $total = $num1 * $num2;
  4.         return $total; 
  5.     }
  6.     $result = calc(42, 0);
  7.     echo $result;    
  8. ?>

a) Error
b) 0
c) 42
d) 84
Answer: b
Clarification: Function returns $total.

9. What will be the output of the following PHP code?

  1.     $var = 10;
  2.     function one()
  3.     {
  4.         echo $var;
  5.     }
  6.     one();
  7. ?>

a) Error
b) 10
c) No Output
d) None of the Mentioned
Answer: c
Clarification: $var is not global and hence is not available for one().

10. What will be the output of the following PHP code?

  1.     function mine($m)
  2.     {
  3.         if ($m < 0)
  4.             echo "less than 0";
  5.         if ($ >= 0)
  6.             echo "Not True";
  7.     }
  8.     mine(0);
  9. ?>

a) Less Than 0
b) Not True
c) No Output
d) None of the Mentioned
Answer: b
Clarification: Argument is 0.

PHP for Aptitude test,

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top