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 calc($price, $tax="")
  3.     {
  4.         $total = $price + ($price * $tax);
  5.         echo "$total"; 
  6.     }
  7.     calc(42);    
  8.     ?>

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

View Answer

Answer: c [Reason:] You can designate certain arguments as optional by placing them at the end of the list and assigning them a default value of nothing.

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

  1.     <?php
  2.     function a()
  3.     {
  4.         function b()
  5.         {
  6.             echo 'I am b';
  7.         }
  8.         echo 'I am a';
  9.     }
  10.     a();
  11.     a();
  12.     ?>

a) I am b
b) I am bI am a
c) Error
d) I am a Error

View Answer

Answer: d [Reason:] This will be the output- I am a Fatal error: Cannot redeclare b()

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

  1.     <?php
  2.     function a()  
  3.     {
  4.         function b()
  5.         {
  6.             echo 'I am b';
  7.         }
  8.         echo 'I am a';
  9.     }
  10.     b();
  11.     a();
  12.     ?>

a) I am b
b) I am bI am a
c) Error
d) I am a Error

View Answer

Answer: c [Reason:] This will be the output- Fatal error: Call to undefined function b(). You cannot call a function which is inside a function without calling the outside function.

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

  1.     <?php
  2.     $op2 = "blabla";
  3.     function foo($op1)
  4.     {
  5.         echo $op1;
  6.         echo $op2;
  7.     }
  8.     foo("hello");
  9.     ?>

a) helloblabla
b) error
c) hello
d) helloblablablabla

View Answer

Answer: c [Reason:] If you want to put some variables in function that was not passed by it, you must use “global”. Inside the function type global $op2.

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

  1.     <?php
  2.         function foo($msg)
  3.         {
  4.             echo "$msg";
  5.         }
  6.         $var1 = "foo";
  7.         $var1("will this work");
  8.     ?>

a) error
b) $msg
c) 0
d) will this work

View Answer

Answer: d [Reason:]It is possible to call a function using a variable which stores the function name.

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

  1.     <?php
  2.        echo "chr(52)";
  3.     ?>

a) 1
b) 2
c) 3
d) 4

View Answer

Answer: d [Reason:] The chr() function returns a character from the specified ASCII value. Since the ASCII value of 4 is 52, thus 4 was displayed.

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

  1.     <?php
  2.         echo ord ("hi");
  3.     ?>

a) 106
b) 103
c) 104
d) 209

View Answer

Answer: c [Reason:] The ord() function returns the ASCII value of the first character of a string. The ASCII value of h is 104, thus 104 was displayed.

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

  1.   <?php
  2.       echo(atan(0.50));
  3.   ?>

a) 0.11845976421345
b) 0.23568451142521
c) 0.46364760900081
d) 1

View Answer

Answer: c [Reason:] The atan() function returns the arc tangent of arg as a numeric value between -Pi/2 and Pi/2 radians.

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

  1.    <?php
  2.        define("GREETING","Hello you! How are you today?");
  3.        echo constant("GREETING");
  4.    ?>

a) Hello you! How are you today?
b) GREETING
c) GREETING, Hello you! How are you today?
d) “GREETING”,”Hello you! How are you today?”

View Answer

Answer: a [Reason:] The define() function defines a constant.

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

  1.     <?php
  2.         define("GREETING1","Hello you! How are you today?");
  3.         define("GREETING2","Hello you! How are you today?");
  4.         define("GREETING3","Hello you! How are you today?");
  5.         echo defined("GREETING");
  6.      ?>

a) 1
b) 0
c) 3
d) 4

View Answer

Answer: b [Reason:] The defined() function checks whether a constant exists.

PHP MCQ Set 2

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

  1. <?php
  2.     function sum($num1, $num2)
  3.     {
  4.         $total = $num1 + $num2;
  5.         echo "chr($total)"; 
  6.     }
  7.     $var1 = "sum";
  8.     $var1(5, 44);    
  9. ?>

a) Error
b) 49
c) 1
d) Sum

View Answer

Answer: c [Reason:] It is possible to call a function using a variable which stores the function name also the chr() function returns a character from the specified ASCII value.

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

  1. <?php
  2.     function sum($num1, $num2)
  3.     {
  4.         $total = $num1 + $num2;
  5.         echo "cos($total)"; 
  6.     }
  7.     sum(5,-5);    
  8. ?>

a) 0
b) 1
c) 0.5
d) -0.5

View Answer

Answer: b [Reason:] cos() gives the cos value of the argument. Here the function returns 1.

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

  1. <?php
  2.     function b()
  3.     {
  4.         echo "b is executed";
  5.     }
  6.     function a()
  7.     {
  8.         b();
  9.         echo "a is executed";
  10.         b();
  11.     }
  12.     a();
  13.   ?>

a) b is executedb is executedb is executed
b) b is executeda is executed
c) a is executed
d) b is executeda is executedb is executed

View Answer

Answer: d [Reason:] Simple order of execution.

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

  1. <?php
  2. function sum($x, $y)
  3. {
  4.     $z = $x + $y;
  5.     return $z;
  6. }
  7. echo "5 + 10 = " . sum(7,13) . "<br>";
  8. echo "7 + 13 = " . sum(2,4) . "<br>";
  9. echo "2 + 4 = " . sum(5,10);
  10. ?>

a) 5 + 10 = 15
2 + 4 = 6
7 + 13 = 20
b) 7 + 13 = 20
5 + 10 = 15
2 + 4 = 6
c) 5 + 10 = 15
7 + 13 = 20
2 + 4 = 6
d) 5 + 10 = 20
7 + 13 = 6
2 + 4 = 15

View Answer

Answer: d [Reason:] The function calls are jumbled.

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

  1. <?php
  2. function addFive($num)
  3. {
  4.     $num += 5;
  5. }
  6. function addSix(&$num)
  7. {
  8.     $num += 6;
  9. }
  10. $orignum = 10;
  11. addFive( &$orignum );
  12. echo "Original Value is $orignum<br />";
  13. addSix( $orignum );
  14. echo "Original Value is $orignum<br />";
  15. ?>

a) Original Value is 15
Original Value is 21
b) Original Value is 15
Original Value is 21
c) Original Value is 15
Original Value is 15
d) None Of The mentioned

View Answer

Answer: b [Reason:] addSix() passes value of the variable by reference.

6. 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:] Functions returns value 30.

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

  1. <?php
  2. function sayHello()
  3. {
  4.    echo "HelloWorld<br />";
  5. }
  6. $function_holder = "sayHello";
  7. $function_holder();
  8. ?>

a) No Output
b) Error
c) sayHello
d) HelloWorld

View Answer

Answer: d [Reason:] It is possible to assign function names as strings to variables and then treat these variables exactly as you would the function name itself.

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

  1. <?php
  2. function one()
  3. {
  4.     echo " this works";
  5.     function two()
  6.     {
  7.         echo "this too works";
  8.     }
  9. }
  10. one();
  11. two();
  12. ?>

a) error
b) this works
c) this worksthis too works
d) this works this too works

View Answer

Answer: c [Reason:] Two is declared in one and is called after one.Hence it works.

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

  1. <?php
  2. function do($myString)
  3. {
  4.     echo strpos($myString, "donkey",0);
  5. }
  6. do("The donkey looks like a horse.");
  7. ?>

a) 4
b) 5
c) 2
d) None of the mentioned

View Answer

Answer: a [Reason:] Donkey starts from position 4 in string.

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

  1. <?php
  2. function one()
  3. {
  4.     define("const","I am awesome!");
  5.     echo constant("const");
  6. }
  7. one();
  8. ?>

a) I am awesome!!
b) const
c) const, I am awesome!!
d) “const”,”I am awesome!”

View Answer

Answer: a [Reason:] Using the define function to define the constant “const”.

PHP MCQ Set 3

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

  1. <?php
  2. print "echo hello world";
  3. ?>

a) echo hello world
b) hello world
c) nothing
d) error

View Answer

Answer: a [Reason:] The print statement will print what ever is present inside the double-quotes.

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

  1. <?php
  2. $one = 1;
  3. print($one);
  4. print $one;
  5. ?>

a) 01
b) 11
c) 10
d) Error

View Answer

Answer: b [Reason:] Print can be used with or without parantheses.

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

  1. <?php
  2. $cars = array("Volvo", "BMW", "Toyota");
  3. print $cars[2];
  4. ?>

a) Volvo
b) BMW
c) Toyota
d) Error

View Answer

Answer: c [Reason:] Print statement can be used to output a specific array member.

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

  1. <?php
  2. $one = "one";
  3. $two = "two";
  4. print($one$two);
  5. ?>

a) onetwo
b) one
c) nothing
d) error

View Answer

Answer: d [Reason:] The above syntax will produce an error, unlike the echo statement.

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

  1. <?php
  2. $one = "one";
  3. $two = "two";
  4. print($one,$two);
  5. ?>

a) onetwo
b) one, two
c) one
d) error

View Answer

Answer: d [Reason:] The above syntax will produce an error, unlike the echo statement.

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

  1. <?php
  2. $one = "one";
  3. $two = "two";
  4. print("$one$two");
  5. ?>

a) onetwo
b) $one$two
c) one
d) error

View Answer

Answer: a [Reason:] This is same as the echo statement.

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

  1. <?php
  2. $one = "one";
  3. $two = "two";
  4. print($one==$two);
  5. ?>

a) true
b) false
c) nothing
d) error

View Answer

Answer: c [Reason:] Since we are equating two unequal strings we do not get any output.

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

  1. <?php
  2. $one = "one";
  3. $two = "one";
  4. print($one == $two);
  5. ?>

a) true
b) false
c) 1
d) error

View Answer

Answer: c [Reason:] Since both the strings are equal the result 1 is printed on the screen.

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

  1. <?php
  2. print "Hello world!<br>";
  3. print "I'm about to learn PHP!";
  4. ?>

a) Hello world!
I’m about to learn PHP!
b) Hello world! I’m about to learn PHP!
c) Hello world!
I’m about to learn PHP!
d) Error

View Answer

Answer: c [Reason:] Most of the properties of echo and print are same. Strings can contain HTML markup.

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

  1. <?php
  2. print("this"."was"."a"."bad"."idea");
  3. ?>

a) thiswasabadidea
b) this was a bad idea
c) nothing
d) error

View Answer

Answer: a [Reason:] You can use the dot operator like in echo but you can not use the comma operator to do the same.

PHP MCQ Set 4

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

  1. <?php
  2. /*
  3. echo "Hello world";
  4. */
  5. ?>

a) Hello world
b) Nothing
c) Error
d) /*
Hello world
*/

View Answer

Answer: b [Reason:] /* */ is used for commenting multiple lines.

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

  1. <?php
  2. $color = red;
  3. echo "$color" . red ;
  4. ?>

a) red red
b) red
c) error
d) nothing

View Answer

Answer: c [Reason:] Use of undefined constant red.

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

  1. <?php
  2. $color1 = red;
  3. $color2 = green;
  4. echo "$color1"."$color2";
  5. ?>

a) red green
b) red
c) green
d) error

View Answer

Answer: d [Reason:] It has to be $color1 = “red”; and $color2 = “green”; therefore the error.

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

  1. <?php
  2. $color = "red";
  3. $color = "green";
  4. echo "$color";
  5. ?>

a) red
b) green
c) red green
d) error

View Answer

Answer: b [Reason:] The variable contains the last value which has been assigned.

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

  1. <?php
  2. $color1 = "red";
  3. $color2 = "green";
  4. echo "$color1" . "$color2";
  5. ?>

a) red
b) green
c) red green
d) redgreen

View Answer

Answer: d [Reason:] The . operator is used to join to strings.

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

  1. <?php
  2. $color1 = "red";
  3. $color2 = "green";
  4. echo "$color1" + "$color2";
  5. ?>

a) redgreen
b) red green
c) 0
d) error

View Answer

Answer: c [Reason:] + operator does not join both the strings.

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

  1. <?php
  2. $color1 = "red";
  3. $color2 = "red";
  4. echo "$color1" + "$color2";
  5. ?>

a) redgreen
b) red green
c) 0
d) 1

View Answer

Answer: c [Reason:] + does not return 1 if the variables are equal.

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

  1. <?php
  2. $color1 = "red";
  3. $color2 = "1";
  4. echo "$color1" + "$color2";
  5. ?>

a) red1
b) red 1
c) 0
d) 1

View Answer

Answer: d [Reason:] + just returns the numeric value even though it is inside double quotes.

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

  1. <?php
  2. $color1 = "1";
  3. $color2 = "1";
  4. echo "$color1" + "$color2";
  5. ?>

a) 11
b) 2
c) 0
d) 1

View Answer

Answer: b [Reason:] + can be used to add to integer values which are enclosed by double-quotes.

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

  1. <?php
  2. $color1 = "red";
  3. $color2 = "1";
  4. $color3 = "grey"
  5. echo "$color1" + "$color2" . "$color3";
  6. ?>

a) 1grey
b) grey
c) 0
d) red1grey

View Answer

Answer: a [Reason:] + gives the value 1 and . is used to give join 1 and grey.

PHP MCQ Set 5

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

  1. <?php
  2. $x = 5;
  3. $y = 10;
  4. $z = "$x + $y";
  5. echo "$z";
  6. ?>

a) 15
b) 10 + 5
c) $z
d) $x + $y

View Answer

Answer: b [Reason:] Variable z will store 10 + 5 because 10 + 5 is given in double-quotes.

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

  1. <?php
  2. $x = 4;
  3. $y = 3;
  4. $z = 1;
  5. echo "$x = $x + $y + $z";
  6. ?>

a) 4 = 4 + 3 + 1
b) 8
c) 8 = 4 + 3 +1
d) Error

View Answer

Answer: a [Reason:] Again since the variables are inside double quotes we get this result.

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

  1. <?php
  2. $x = 4;
  3. $y = 3
  4. $z = 1;
  5. $z = $z + $x + $y;
  6. echo "$z";
  7. ?>

a) $z
b) 15
c) 8
d) 1

View Answer

Answer: c [Reason:] Normal addition of variables x, y and z occurs and result of 8 will be displayed.

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

  1. <?php
  2. $x = 3.3;
  3. $y = 2;
  4. echo $x % $y;
  5. ?>

a) 0
b) 1
c) 2
d) Error

View Answer

Answer: b [Reason:] % is the modulo operator. Unlike in C we can use it get reminder or floating point numbers in PHP.

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

  1. <?php
  2. $x = 10;
  3. $y = 4;
  4. $z = 3;
  5. echo $x % $y % $z;
  6. ?>

a) 0
b) 1
c) 2
d) Error

View Answer

Answer: c [Reason:] The expression is considered as ($x%$y)%z in this case (10%4)%3 which is 2.

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

  1. <?php
  2. $x = 10;
  3. $y = 4;
  4. $z = 3;
  5. echo ($x % ($y) + $z);
  6. ?>

a) 5
b) 3
c) 0
d) 1

View Answer

Answer: a [Reason:] The inner most bracket is evaluated first, since it covers only variable y it is as good as not using brackets.

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

  1. <?php
  2. $x = 30;
  3. $y = 20;
  4. $z = 10;
  5. echo $x + $y - $z / ($z - $y);
  6. ?>

a) 41
b) -4
c) -5
d) 51

View Answer

Answer: d [Reason:] First ($z – $y) is evalulated then -$z/($z – $y) is evaluated this results in 1 which is added to $x + $y therefore we get 51.

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

  1. <?php
  2. $x = -1;
  3. $y = 1;
  4. $z = $x * $y + $z;
  5. echo $z;
  6. ?>

a) Undefined variable z
b) -1
c) Undefined variable z
-1
d) None of the mentioned

View Answer

Answer: c [Reason:] Since the variable z is not defined it returns the error also it takes z as 0 and returns the value -1.

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

  1. <?php
  2. $x = 4;
  3. $y = -3;
  4. $z = 11;
  5. echo 4 + $y * $z / $x;
  6. ?>

a) 4.25
b) 3.25
c) -3.25
d) -4.25

View Answer

Answer: d [Reason:] First the * is evaluated then / followed by + therefore we can rewrite this expression as 4 +((- 3 * 11) / 4) which results in -4.25.

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

  1. <?php
  2. $x = 3.5;
  3. $y = 2;
  4. $z = 2;
  5. echo $x / $y / $z;
  6. ?>

a) 1.75
b) 0.875
c) 3.5
d) Error

View Answer

Answer: b [Reason:] First $x / $y is evaluated then this is divided by $z therefore we get 0.875.

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