PHP Coding Questions and Answers – Functions – 4

This set of PHP Questions and Answers for Aptitude test focuses on “Functions – 4”.

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

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

a) Case 1 was executed
b) Case 2 was executed
c) Default was executed
d) Case 4 was executed
View Answer

Answer: d
Explanation: The switch statement is executed with $x = 9.
advertisement
advertisement

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

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

a) TIME TO LIVE KING SIZE
b) Time to live king size
c) Uppercase
d) Time To Live King Size
View Answer

Answer: d
Explanation: 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?

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

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
View Answer

Answer: b
Explanation: b is undeclared if TV() is not called first.
advertisement

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

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

Answer: a
Explanation: This one works because b is declared independent of TV() also.

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

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

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
View Answer

Answer: b
Explanation: Function b is declared twice.

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

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

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
View Answer

Answer: c
Explanation: b is declared as TV() is executed first.

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

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

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

Answer: a
Explanation: Function does not return anything.

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

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

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

Answer: b
Explanation: Function returns $total.

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

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

a) Error
b) 10
c) No Output
d) None of the Mentioned
View Answer

Answer: c
Explanation: $var is not global and hence is not available for one().

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

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

a) Less Than 0
b) Not True
c) No Output
d) None of the Mentioned
View Answer

Answer: b
Explanation: Argument is 0.

Sanfoundry Global Education & Learning Series – PHP Programming.

To practice all areas of PHP for Aptitude test, here is complete set of 1000+ Multiple Choice Questions and Answers on PHP.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.