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. PHP has long supported two regular expression implementations known as _______ and _______
i) Perl
ii) PEAR
iii) Pearl
iv) POSIX
a) i) and ii)
b) ii) and iv)
c) i) and iv)
d) ii) and iii)

View Answer

Answer: c [Reason:] None.

2. Which one of the following regular expression matches any string containing zero or one p?
a) p+
b) p*
c) P?
d) p#

View Answer

Answer: c [Reason:] None.

3. [:alpha:] can also be specified as..
a) [A-Za-z0-9].
b) [A-za-z].
c) [A-z].
d) [a-z].

View Answer

Answer: b [Reason:][:alpha:] is nothing but Lowercase and uppercase alphabetical characters.

4. How many functions does PHP offer for searching strings using POSIX style regular expression?
a) 7
b) 8
c) 9
d) 10

View Answer

Answer: a [Reason:] ereg(), ereg_replace(), eregi(), eregi_replace(), split(), spliti(), and sql_regcase() are the functions offered.

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

  1.     <?php
  2.     $username = "jasoN";
  3.     if (ereg("([^a-z])",$username))
  4.         echo "Username must be all lowercase!";
  5.     else
  6.         echo "Username is all lowercase!";
  7.     ?>

a) Error
b) Username must be all lowercase!
c) Username is all lowercase!
d) No Output is returned

View Answer

Answer: b [Reason:] Because the provided username is not all lowercase, ereg() will not return FALSE (instead returning the length of the matched string, which PHP will treat as TRUE), causing the message to output.

6. POSIX implementation was deprecated in which version of PHP?
a) PHP 4
b) PHP 5
c) PHP 5.2
d) PHP 5.3

View Answer

Answer: d [Reason:] None.

7. POSIX stands for
a) Portable Operating System Interface for Unix
b) Portable Operating System Interface for Linux
c) Portative Operating System Interface for Unix
d) Portative Operating System Interface for Linux

View Answer

Answer: a [Reason:] None.

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

  1.     <?php
  2.     $text = "this istsome text thatnwe might like to parse.";
  3.     print_r(split("[nt]",$text));
  4.     ?>

a) this is some text that we might like to parse.
b) Array ( [0] => some text that [1] => we might like to parse. )
c) Array ( [0] => this is [1] => some text that [2] => we might like to parse. )
d) [0] => this is [1] => some text that [2] => we might like to parse.

View Answer

Answer: d [Reason:] The split() function divides a string into various elements, with the boundaries of each element based on the occurrence of a defined pattern within the string.

9. Which of the following would be a potential match for the Perl-based regular expression /fo{2,4}/ ?
i) fol
ii) fool
iii) fooool
iv) fooooool
a) Only i)
b) ii) and iii)
c) i), iii) and iv)
d) i) and iv)

View Answer

Answer: b [Reason:] This matches f followed by two to four occurrences of o.

10. Which among the following is/are not a metacharacter?
i) a
ii) A
iii) b
iv) B
a) Only i)
b) i) and iii)
c) ii), iii) and iv)
d) ii) and iv)

View Answer

Answer: a [Reason:] /A, /b and /B are metacharacters. A: Matches only at the beginning of the string. b: Matches a word boundary. B: Matches anything but a word boundary.

PHP MCQ Set 2

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

  1. <?php 
  2. $x = 75;
  3. $y = 25; 
  4. function addition()
  5. {
  6.     $GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];
  7. }
  8. addition();
  9. echo $z;
  10. ?>

a) 100
b) error
c) 75
d) 25

View Answer

Answer: a [Reason:] z is a variable present within the $GLOBALS array, it is also accessible from outside the function!

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

  1. <?php
  2. function 2myfunc()
  3. {
  4.     echo "Hello World";
  5. }
  6. 2myfunc();
  7. ?>

a) Hello World
b) No Output
c) ERROR
d) None of the mentioned

View Answer

Answer: c [Reason:] Function cannot begin with a number.

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

  1. <?php
  2. function _func()
  3. {
  4.     echo "Hello World";
  5. }
  6. _func();
  7. ?>

a) Hello World
b) No Output
c) ERROR
d) None of the mentioned

View Answer

Answer: a [Reason:] Function Begining with “_” is valid)

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

  1. <?php
  2. function test($int)
  3. {
  4.     if ($int == 1)
  5.         echo "This Works";
  6.     if ($int == 2)
  7.         echo "This Too Seems To Work";
  8. }
  9. test(1);
  10. TEST(2);
  11. ?>

a) This Works
b) This Too Seems To Work
c) This WorksThis Too Seems To Work
d) ERROR

View Answer

Answer: c [Reason:] Function Is case Insensitive.

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

  1. <?php
  2. function mine($num)
  3. {
  4.     $num = 2 + $num;
  5. echo $num;
  6. }
  7. mine(3);
  8. ?>

a) 3
b) $num
c) 5
d) None of the mentioned

View Answer

Answer: c [Reason:] Simple arithmetic operation.

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

  1. <?php
  2. function mine($num)
  3. {
  4.     $num = 2 + $num;
  5.     echo "$num";
  6. }
  7. mine(3);
  8. ?>

a) 3
b) $num
c) 5
d) None of the mentioned

View Answer

Answer: b [Reason:] The function is defined as echo “$num”.This means $num is treated as a string and not as a variable.

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

  1. <?php
  2. function one($string)
  3. {
  4.     echo "I am ". $String;
  5. }
  6. one("Batman");
  7. ?>

a) I
am Batman
b) I am
c) Batman
d) ERROR

View Answer

Answer:d [Reason:] Variable Undeclared) $string is not the same as $String.

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

  1. <?php
  2.     function string($title);
  3.     {
  4.         $title = ucwords($title);
  5.         echo lcfirst($title);
  6.     }
  7.     string("you went full retard");
  8. ?>

a) You went full retard
b) You Went Full Retard
c) YOU WENT FULL RETARD
d) you Went Full Retard

View Answer

Answer:d [Reason:] ucwords() changes all the first letters to capitals. lcfirst() changes first letter of a string to small.

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

  1. <?php
  2.     function multi($num)
  3.     {
  4.         if ($num == 3)
  5.             echo "I Wonder";
  6.         if ($num == 7)
  7.             echo "Which One";
  8.         if ($num == 8)
  9.             echo "Is The";
  10.         if ($num == 19)
  11.             echo "Correct Answer";
  12.     }
  13.     $can = stripos("I love php, I love php too!","PHP");
  14.     multi($can);
  15. ?>

a) I Wonder
b) Which One
c) Is The
d) Correct Answer

View Answer

Answer:b [Reason:] The stripos() function finds the position of the first occurrence of a string inside another string. In this case it returns 7.

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

  1. <?php
  2. function movie($int)
  3. {
  4.     $movies = array("Fight Club", "Kill Bill", "Pulp Fiction");
  5.     echo "You Do Not Talk About ". $movie[$integer];
  6. }
  7. movie(0);
  8. ?>

a) You Do Not Talk About Fight Club
b) You Do Not Talk About Kill Bill
c) You Do Not Talk About Pulp Fiction
d) None of the mentioned

View Answer

Answer:a [Reason:] Simple use of arrays.

PHP MCQ Set 3

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

  1. <?php
  2. $x;
  3. if ($x)
  4.     print "hi" ;
  5. else
  6.     print "how are u";
  7. ?>

a) how are u
b) hi
c) error
d) no output

View Answer

Answer: a [Reason:] Uninitialized x is set to 0, thus if condition fails.

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

  1. <?php
  2. $x = 0;
  3. if ($x++)
  4.     print "hi";
  5. else
  6.     print "how are u";
  7. ?>

a) hi
b) no output
c) error
d) how are u

View Answer

Answer: d [Reason:] x is incremented after if which evaluates to false.

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

  1. <?php
  2. $x;
  3. if ($x == 0)
  4.     print "hi" ;
  5. else
  6.     print "how are u";
  7.     print "hello"
  8. ?>

a) how are uhello
b) hihello
c) hi
d) no output

View Answer

Answer: b [Reason:] else condition without brackets performs the following statements only.

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

  1. <?php
  2. $x = 0;
  3. if ($x == 1)
  4.     if ($x >= 0)
  5.         print "true";
  6.     else
  7.         rint "false"; 
  8. ?>

a) true
b) false
c) error
d) no output

View Answer

Answer: d [Reason:] The nested for loop is not entered if outer condition is false.

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

  1. <?php
  2. $a = 1;
  3. if ($a--)
  4.     print "True";
  5. if ($a++)
  6.     print "False"; 
  7. ?>

a) true
b) false
c) error
d) no output

View Answer

Answer: a [Reason:] Due to post increment and post decrement only the first condition is satisfied.

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

  1. <?php
  2. $a = 1;
  3. if (echo $a)
  4.     print "True";
  5. else
  6.     print "False"; 
  7. ?>

a) true
b) false
c) error
d) no output

View Answer

Answer: c [Reason:] echo does not return anything so if condition is empty.

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

  1. <?php
  2. $a = 1;
  3. if (print $a)
  4.     print "True";
  5. else
  6.     print "False"; 
  7. ?>

a) true
b) false
c) error
d) no output

View Answer

Answer: a [Reason:] print returns 1 if it prints anything.

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

  1. <?php
  2. $a = 10;
  3. if (1) 
  4.     print "all";
  5. else 
  6.     print "some"
  7. else 
  8.     print "none";
  9. ?>

a) all
b) some
c) error
d) none

View Answer

Answer: c [Reason:] Hanging else statement.

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

  1. <?php
  2. $a = 10;
  3. if (0) 
  4.     print "all";
  5.  if 
  6.  else 
  7.      print "some"
  8. ?>

a) all
b) some
c) error
d) no output

View Answer

Answer: c [Reason:] No else statement to end the if statement.

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

  1. <?php
  2. $a = "";
  3. if ($a) 
  4.     print "all";
  5. if 
  6. else 
  7.     print "some";
  8. ?>

a) all
b) some
c) error
d) no output

View Answer

Answer: b [Reason:] Empty string is evaluated to 0.

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

  1. <?php
  2. $a = "a";
  3. if ($a) 
  4.     print "all";
  5. else 
  6.     print "some";
  7. ?>

a) all
b) some
c) error
d) no output

View Answer

Answer: a [Reason:] The value of a is evaluated to 1 as it has a value.

PHP MCQ Set 4

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

  1. <?php
  2. "Hello World"
  3. ?>

a) Error
b) Hello World
c) Nothing
d) Missing semicolon error

View Answer

Answer: c [Reason:] If you need to output something onto the screen you’ll need to use echo or print_r.

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

  1. <?php
  2. print_r "Hello world"
  3. ?>

a) Error
b) Hello World
c) Nothing
d) Missing semicolon error

View Answer

Answer: a [Reason:] The statement should be print_r(‘Hello World’) to print Hello world. Also if there is only one line then there is no requirement of a semicolon, but it is better to use it.

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

  1. <?php
  2. echo 'Hello World';
  3. <html>
  4. Hello world
  5. </html>
  6. ?>

a) Hello world
b) Hello World Hello World
c) Hello world
Hello World
d) Syntax Error

View Answer

Answer: d [Reason:] Parse error: syntax error, unexpected ‘<‘ on line 2. You can not use the html tag inside php tags.

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

  1. <?php
  2. Echo "Hello World1";
  3. echo " Hello world2";
  4. ECHO " Hello world3";
  5. ?>

a) Hello world1 Hello world2 Hello World3
b) Hello world1
Hello world2
Hello World3
c) Error
d) Hello world1 Hello world3

View Answer

Answer: a [Reason:] In PHP, all user-defined functions, classes, and keywords (e.g. if, else, while, echo, etc.) are case-insensitive.

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

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

a) redredred
b) redred
c) red
d) Error

View Answer

Answer: c [Reason:] In PHP, all variables are case-sensitive.

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

  1. <?php
  2.  # echo "Hello world";
  3.  echo "# Hello world"; 
  4. ?>

a) # Hello world
b) Hello world# Hello world
c) Hello world
d) Error

View Answer

Answer: a [Reason:] # is a single line comment.

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

  1. <?php
  2. echo "<i>Hello World</i>"
  3. ?>

a) Hello world
b) Hello world in italics
c) Nothing
d) Error

View Answer

Answer: b [Reason:] You can use tags like italics, bold etc. inside php script.

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

  1. <?php
  2. echo "echo "Hello World"";
  3. ?>

a) Hello world
b) echo “Hello world”
c) echo Hello world
d) Error

View Answer

Answer: d [Reason:] It would have printed echo “Hello world” if the statement was echo “echo ”Hello World””;.

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

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

a) HELLO WORLD
b) Hello world
c) Nothing
d) Error

View Answer

Answer: d [Reason:] You can not have php tags inside a php tag.

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

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

a) red
b) $color
c) red
d) Error

View Answer

Answer:b [Reason:] To print red remove the .

PHP MCQ Set 5

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

  1. <?php
  2. echo 5 * 9 / 3 + 9;
  3. ?>

a) 24
b) 3.7
c) 3.85
d) 0

View Answer

Answer: a [Reason:] Operator precedence order must be followed.

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

  1. <?php
  2. echo 5 * 9 / 3 + 9
  3. ?>

a) 24
b) 3.7
c) 3.85
d) 0

View Answer

Answer: a [Reason:] Operator precedence order must be followed.

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

  1. <?php
  2. $i = 0;
  3. $j = 0;
  4. if ($i && ($j = $i + 10)) {
  5.     echo "true";
  6. }
  7. echo $j;
  8. ?>

a) 10
b) 0
c) true0
d) true10

View Answer

Answer: b [Reason:] In if condition when the first case is 0 and is an && operation then the second command is not executed.

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

  1. <?php
  2. $i = 10;
  3. $j = 0;
  4. if ($i || ($j = $i + 10)) {
  5.     echo "true";
  6. }
  7. echo $j;
  8. ?>

a) 20
b) true0
c) 0
d) true20

View Answer

Answer: b [Reason:] In if condition when the first case is 1 and is an || operation then the second command is not executed.

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

  1. <?php
  2. $i = 1;
  3. if ($i++ && ($i == 1))
  4.     printf("Yesn$i");
  5. else
  6.     printf("Non$i");
  7. ?>

a) No 2
b) Yes 1
c) Yes 2
d) No 1

View Answer

Answer: a [Reason:] The first condition returns true and increments but the second condition is false.

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

  1. <?php
  2. $a = 1; $b = 3;
  3. $d = $a++ + ++$b;
  4. echo $d;
  5. ?>

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

View Answer

Answer: a [Reason:] Post increment of a is done after expression evaluation.

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

  1. <?php
  2. $a = 1; $b = 1; $d = 1;
  3. print ++$a + ++$a+$a++; print $a++ + ++$b; print ++$d + $d++ + $a++;
  4. ?>

a) 869
b) 742
c) 368
d) error

View Answer

Answer: a [Reason:] Follow the order of post and pre increments.

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

  1. <?php
  2. $a = 10; $b = 10;
  3. if ($a = 5)
  4.     $b--;
  5. print $a;print $b--;
  6. ?>

a) 58
b) 59
c) 109
d) 108

View Answer

Answer: b [Reason:] a is set to 5 in the if condition and b is postdecremented in the print statement.

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

  1. <?php
  2. $i = 0;
  3. $x = $i++; $y = ++$i;
  4. print $x; print $y; 
  5. ?>

a) 02
b) 12
c) 01
d) 21

View Answer

Answer: a [Reason:] First case i is incremented after setting x to i.

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

  1. <?php
  2.  $a = 5; $b = -7; $c =0; 
  3.  $d = ++$a && ++$b || ++$c;
  4.  print $d; print $a;
  5. ?>

a) 16
b) 06
c) 15
d) 05

View Answer

Answer: a [Reason:] 1&&0||1 is evaluated to 1 and the a is also preincremented to 6.

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

  1. <?php
  2. $b = 1; $c = 4; $a = 5; 
  3. $d = $b + $c == $a;
  4. print $d;
  5. ?>

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

View Answer

Answer: d [Reason:] First b and c are added and then tested if d=5,which is true thus return 1.

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