Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
Filter by Categories
nmims post
Objective Type Set
Online MCQ Assignment
Question Solution
Solved Question
Uncategorized

PHP MCQ Set 1

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

  1. <?php
  2. function colour()
  3. { 
  4.     $colors = array("red", "green", "blue", "yellow"); 
  5.     foreach ($colors as $value)
  6.     {
  7.         echo "$value <br>";
  8.     }
  9. }
  10. colour();
  11. ?>

a) red
green
blue
yellow
b) green
blue
yellow
red
c) red
blue
yellow
green
d) red
green
yellow
blue

View Answer

Answer:a [Reason:] For each function causes the program to loop through each array value once.

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

  1. <?php
  2. function addFunction($num1, $num2)
  3. {
  4.     $sum = $num1 + $num2;
  5.     return $sum;
  6. }
  7. $return_value = addFunction(10, 20);
  8. echo "Returned value from the function : " .$return_value
  9. ?>

a) Returned value from the function : $return_value
b) Error
c) Returned value from the function : 30
d) Returned value from the function :

View Answer

Answer: c [Reason:] Function returns 30.

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

  1. <?php
  2. function time($string)
  3. {
  4.     echo strtr("Towe Pa55", "ow5", $string);
  5. }
  6. time("ims");
  7. ?>

a) Time Pa55
b) Towe Pa55
c) Towe Pass
d) Time Pass

View Answer

Answer:d [Reason:] The strtr() function translates certain characters in a string.

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

  1. <?php
  2. function constant()
  3. {
  4.     define("GREETING", "Welcome to Narnia");
  5.     echo greeting;
  6. }
  7. ?>

a) Welcome to Narnia
b) greeting
c) GREETING
d) ERROR

View Answer

Answer:d [Reason:] By default constants are case sensitive. Hence an error will arise.

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

  1. <?php
  2. function constant()
  3. {
  4.     define("GREETING", "Welcome to Narnia",true);
  5.     echo greeting;
  6. }
  7. ?>

a) Welcome to Narnia
b) greeting
c) GREETING
d) ERROR

View Answer

Answer:a [Reason:] By default constants are case sensitive. But the third parameter in define(), if set to true, makes constants case insensitive.

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

  1. <?php
  2. function start($string)
  3. {
  4.     if ($string < 45)
  5.         return 20;
  6.     else
  7.         return 40;
  8. }
  9. $t = start(90);
  10. if ($t < 20)
  11. {
  12.     echo "Have a good day!";
  13. }
  14. else
  15. {
  16.     echo "Have a good night!";
  17. }
  18. ?>

a) Have a good day!
b) Have a good night!
c) ERROR
d) None of the mentioned

View Answer

Answer: b [Reason:] Function returns 40. this is greater than 20, hence the output.

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

  1. <?php
  2. function case()
  3. {
  4.     ECHO "Hello World!<br>";
  5.     echo "Hello World!<br>";
  6.     EcHo "Hello World!<br>";
  7. }
  8. case();
  9. ?>

a) Hello World!
b) Hello World!
Hello World!
c) Hello World!
Hello World!
Hello World!
d) None of the mentioned

View Answer

Answer:c [Reason:] Functions, keywords etc) in php are case insensitive.

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

  1. <?php
  2. function email()
  3. {
  4.     $email = ’user@yahoo.com’;
  5.     $new = strstr($email,@');
  6.     echo $new;
  7. }
  8. email();
  9. ?>

a) user
b)
c) @yahoo.com
d) yahoo.com

View Answer

Answer: c [Reason:] The strstr() function searches for the first occurrence of a string inside another string.

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

  1. <?php
  2. function string()
  3. {
  4.     echo strstr("Hello world!", 111);
  5. }
  6. string();
  7. ?>

a) o world!
b) Hello world!
c) 111
d) No Output

View Answer

Answer:a [Reason:] 111 is the ASCII value of o.

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

  1. <?php
  2. function CalAll($x,$y)
  3. {
  4.     echo ($x + $y);
  5.     echo "<br>";
  6.     echo ($x - $y);
  7.     echo "<br>";
  8.     echo ($x * $y);
  9.     echo "<br>";
  10.     echo ($x / $y); 
  11.     echo "<br>";
  12.     echo ($x % $y);
  13. }
  14. $x = 10; 
  15. $y = 6;
  16. CalcAll(); 
  17. ?>

a) 4
60
1.6666666666667
4
16
b) 16
4
60
1.6666666666667
4
c) 4
16
4
60
1.6666666666667
d) 1.6666666666667
4
16
4
60

View Answer

Answer:b [Reason:] Simple usage of all arithmetic operators.

PHP MCQ Set 2

1. Which of the following advanced OOP features is/are not supported by PHP?
i) Method overloading
ii) Multiple Inheritance
iii) Namespaces
iv) Object Cloning
a) All of the mentioned
b) None of the mentioned
c) i) and ii)
d) iii) and iv)

View Answer

Answer: c [Reason:] The advanced OOP features are: Object cloning, Inheritance, Interfaces, Abstract classes, and Namespaces.

2. Which version of PHP introduced the advanced concepts of OOP?
a) PHP 4
b) PHP 5
c) PHP 5.3
d) PHP 6

View Answer

Answer: b [Reason:] None.

3. Which one of the following is the right way to clone an object?
a) _clone(targetObject);
b) destinationObject = clone targetObject;
c) destinationObject = _clone(targetObject);
d) destinationObject = clone(targetObject);

View Answer

Answer: b [Reason:] You can clone an object by prefacing it with the clone keyword.

4. The class from which the child class inherits is called..
i) Child class
ii) Parent class
iii) Super class
iv) Base class
a) Only i)
b) ii), iii) and iv)
c) Only iii)
d) ii) and iv)

View Answer

Answer: d [Reason:] None.

5. Which of the following is/are true for an abstract class?
i) A class is declared abstract by prefacing the definition with the word abstract.
ii) A class is declare abstract by using the keyword implements.
iii) It is a class that really isn’t supposed to ever be instantiated but instead serves as a base class.
iv) Attempting to instantiate an abstract class results in an error.
a) Only ii)
b) All of the mentioned
c) ii) and iv)
d) ii), iii) and iv)

View Answer

Answer: a [Reason:] None.

6. If one intends to create a model that will be assumed by a number of closely related objects, which class must be used?
a) Normal class
b) Static class
c) Abstract class
d) Interface

View Answer

Answer: c [Reason:] None.

7. If your object must inherit behavior from a number of sources you must use a/an
a) Interface
b) Object
c) Abstract class
d) Static class

View Answer

Answer: a [Reason:] None.

8. Which method is used to tweak an object’s cloning behavior?
a) clone()
b) __clone()
c) _clone
d) object_clone()

View Answer

Answer: b [Reason:] None.

9. Which feature allows us to call more than one method or function of the class in single instruction?
a) Typecasting
b) Method Including
c) Method adding
d) Method chaining

View Answer

Answer: d [Reason:] Following is a basic example of method chaining in php: $a = new Order(); $a->CreateOrder()->sendOrderEmail()->createShipment();

10. Which magic method is used to implement overloading in PHP?
a) __call
b) __invoke
c) __wakeup
d) __unset

View Answer

Answer: a [Reason:] None.

PHP MCQ Set 3

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 [Reason:] The switch statement is executed with $x = 9.

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 [Reason:] 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. <?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 [Reason:] b is undeclared if TV() is not called first.

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 [Reason:] 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 [Reason:] 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 ro spoil this code
d) None of the mentioned

View Answer

Answer:c [Reason:] 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 [Reason:] 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 [Reason:] 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 [Reason:] $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 [Reason:] Argument is 0.

PHP MCQ Set 4

1. PHP’s numerically indexed array begin with position ___________
a) 1
b) 2
c) 0
d) -1

View Answer

Answer: c [Reason:] Like many programming languages, the first element of an array has an index value of 0.

2. Which of the following are correct ways of creating an array?
i) state[0] = “karnataka”;
ii) $state[] = array(“karnataka”);
iii) $state[0] = “karnataka”;
iv) $state = array(“karnataka”);
a) iii) and iv)
b) ii) and iii)
c) Only i)
d) ii), iii) and iv)

View Answer

Answer: a [Reason:] A variable name should start with $ symbol which is not present in i) and you need not put the square brackets when you use the array() constructor.

3. What will be the output of the following php code?

  1.     <?php
  2.     $states = array("karnataka" => array
  3.     ( "population" => "11,35,000", "captial" => "Bangalore"),
  4.     "Tamil Nadu" => array( "population" => "17,90,000",
  5.     "captial" => "Chennai") );
  6.     echo $states["karnataka"]["population"];
  7.     ?>

a) karnataka 11,35,000
b) 11,35,000
c) population 11,35,000
d) karnataka population

View Answer

Answer: b [Reason:] Treat states as a multidimensional array and accordingly traverse it to get the value.

4. Which function will return true if a variable is an array or false if it is not?
a) this_array()
b) is_array()
c) do_array()
d) in_array()

View Answer

Answer: b [Reason:] A built-in function, is_array(), is available for testing an array. Its prototype follows: boolean is_array(mixed variable).

5. Which in-built function will add a value to the end of an array?
a) array_unshift()
b) into_array()
c) inend_array()
d) array_push()

View Answer

Answer: d [Reason:] array_push adds a value to the end of an array, returning the total count of elementsin the array after the new value has been added.

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

  1.     <?php
  2.     $state = array ("Karnataka", "Goa", "Tamil Nadu",
  3.     "Andhra Pradesh");
  4.     echo (array_search ("Tamil Nadu", $state) );
  5.     ?>

a) True
b) 1
c) False
d) 2

View Answer

Answer: d [Reason:] The array_search() function searches an array for a specified value, returning its key if located and FALSE otherwise.

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

  1.     <?php
  2.     $fruits = array ("apple", "orange", "banana");
  3.     echo (next($fruits));	
  4.     echo (next($fruits));
  5.     ?>

a) orangebanana
b) appleorange
c) orangeorange
d) appleapple

View Answer

Answer: a [Reason:] The next() function returns the array value residing at the position immediately following that of the current array pointer.

8. Which function can be used to move the pointer to the previous array position?
a) last()
b) before()
c) prev()
d) previous()

View Answer

Answer: c [Reason:] None.

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

  1.     <?php
  2.     $fruits = array ("apple", "orange", array ("pear", "mango"),
  3.     "banana");
  4.     echo (count($fruits, 1));
  5.     ?>

a) 3
b) 4
c) 5
d) 6

View Answer

Answer: d [Reason:] The array entity holding pear and mango is counted as an item, just as its contents are.

10. Which function returns an array consisting of associative key/value pairs?
a) count()
b) array_count()
c) array_count_values()
d) count_values()

View Answer

Answer: c [Reason:] None.

PHP MCQ Set 5

1. What does PHP stand for?
i) Personal Home Page
ii) Hypertext Preprocessor
iii) Pretext Hypertext Processor
iv) Preprocessor Home Page
a) Both i) and iii)
b) Both ii) and iv)
c) Only ii)
d) Both i) and ii)

View Answer

Answer: d [Reason:] PHP previously stood for Personal Home Page now stands for Hypertext Preprocessor.

2. PHP files have a default file extension of_______
a) .html
b) .xml
c) .php
d) .ph

View Answer

Answer: c [Reason:] None.

3. A PHP script should start with ___ and end with ___
a) < php >
b) < ? php ?>
c) <? ?>
d) <?php ?>

View Answer

Answer: c [Reason:] Every section of PHP code starts and ends by turning on and off PHP tags to let the server know that it needs to execute the PHP in between them.

4. Which of the following is/are a PHP code editor?
i) Notepad
ii) Notepad++
iii) Adobe Dreamweaver
iv) PDT
a) Only iv)
b) All of the mentioned.
c) i), ii) and iii)
d) Only iii)

View Answer

Answer: b [Reason:] Any of the above editors can be used to type php code and run it.

5. Which of the following must be installed on your computer so as to run PHP script?
i) Adobe Dreamweaver
ii) PHP
iii) Apache
iv) IIS
a) All of the mentioned.
b) Only ii)
c) ii) and iii)
d) ii), iii) and iv)

View Answer

Answer: d [Reason:] To run PHP code you need to have PHP and a web server, both IIS and Apache are web servers.You can choose either one according to your platform.

6. Which version of PHP introduced Try/catch Exception?
a) PHP 4
b) PHP 5
c) PHP 5.3
d) PHP 6

View Answer

Answer: b [Reason:] Version 5 added support for Exception Handling.

7. We can use ___ to comment a single line?
i) /?
ii) //
iii) #
iv) /* */
a) Only ii)
b) i), iii) and iv)
c) ii), iii) and iv)
d) Both ii) and iv)

View Answer

Answer: c [Reason:] /* */ can also be use to comment just a single line although it is used for paragraphs. // and # are used only for single line comment.

8. Which of the following php statement/statements will store 111 in variable num?
i) int $num = 111;
ii) int mum = 111;
iii) $num = 111;
iv) 111 = $num;
a) Both i) and ii)
b) All of the mentioned
c) Only iii)
d) Only i)

View Answer

Answer: c [Reason:] You need not specify the datatype in php.

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

  1.     <?php
  2.     $num  = 1;
  3.     $num1 = 2;
  4.     print $num . "+". $num1;
  5.     ?>

a) 3
b) 1+2
c) 1.+.2
d) Error

View Answer

Answer: b [Reason:] .(dot) is used to combine two parts of the statement. Example ( $num . “Hello World” ) will output 1Hello World.

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

  1.     <?php
  2.     $num  = "1";
  3.     $num1 = "2";
  4.     print $num+$num1;
  5.     ?>

a) 3
b) 1+2
c) Error
d) 12

View Answer

Answer: a [Reason:] The numbers inside the double quotes are considered as integers and not string, therefore the value 3 is printed and not 1+2.

Synopsis and Project Report

You can buy synopsis and project from distpub.com. Just visit https://distpub.com/product-category/projects/ and buy your university/institute project from distpub.com