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. while()
  3. {
  4.     print "hi";
  5. }
  6. ?>

a) infinite loop
b) hi
c) no output
d) error

View Answer

Answer: d [Reason:] The while loop cannot be defined without a condition.

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

  1. <?php
  2. do
  3. {
  4.     print "hi";
  5. }
  6. while(0);
  7. print "hello";
  8. ?>

a) infinite loop
b) hihello
c) hello
d) error

View Answer

Answer: b [Reason:] The do while loop executes atleast once as the condition is in the while loop.

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

  1. <?php
  2. $i = 0
  3. do
  4. {
  5.     print "hi";
  6.     $i++;
  7. }
  8. while ($i != 3);
  9. ?>

a) hi
hi
b) hi
c) hi
hi
hi
hi
d) no output

View Answer

Answer: c [Reason:] The check happens after the increment,thus it prints until i = 4.

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

  1. <?php
  2. $i = 0
  3. while ($i != 3)
  4. {
  5.     print "hi";
  6. 	$i++;
  7. }
  8. ?>

a) hi
hi
b) hi
hi
hi
c) hi
hi
hi
hi
d) no output

View Answer

Answer: b [Reason:] The check happens before the increment, thus it prints until i = 3.

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

  1. <?php
  2. $i = 0
  3. while ($i < 3)
  4. {
  5.     print "hi";
  6.     $i--;
  7. }
  8. print "hello"
  9. ?>

a) hi
hi
hello
b) hi
hi
hi
hello
c) hi
hi
hi
hi
hello
d) infinite loop

View Answer

Answer: d [Reason:] There is no increment of i making it infinite.

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

  1. <?php
  2. $i = 0
  3. while ($i < 3)
  4. {
  5.     $i++;
  6. }
  7. print $i;
  8. ?>

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

View Answer

Answer: b [Reason:] The increment happens and then the check happens.

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

  1. <?php
  2. $i = 0
  3. do
  4. {
  5.     $i++;
  6. }
  7. while ($i < 3);
  8. print $i;
  9. ?>

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

View Answer

Answer: b [Reason:] The increment happens and then the check happens.

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

  1. <?php
  2. $i = 0
  3. while ($i++)
  4. {
  5.     print $i;
  6. }
  7. print $i;
  8. ?>

a) 0
b) infinite loop
c) 01
d) 1

View Answer

Answer: d [Reason:] As it is a post increment, it checks and then does not enter the loop, thus prints only 1.

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

  1. <?php
  2. $i = "";
  3. while($i)
  4. {   
  5.     print "hi";
  6. }
  7. print "hello";
  8. ?>

a) hello
b) infinite loop
c) hihello
d) error

View Answer

Answer: a [Reason:] While accept does not accept anything other than a 0 or any other number as false and true.

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

  1. <?php
  2. $i = "";
  3. while ($i)
  4. {   
  5.     print "hi";
  6. } 
  7. while($i < 8)
  8.     $i++;
  9. print "hello";
  10. ?>

a) Hi is printed 8 times, hello 7 times and then hi 2 times
b) Hi is printed 10 times, hello 7 times
c) Hi is printed once, hello 7 times
d) Hi is printed once, hello 7 times and then hi 2 times

View Answer

Answer: d [Reason:] The while loop ends only when a } is encountered.

PHP MCQ Set 2

1. Which of following variables can be assigned a value to it?
i) $3hello
ii) $_hello
iii) $this
iv) $This
a) All of the mentioned
b) Only ii)
c) ii), iii) and iv)
d) ii) and iv)

View Answer

Answer: d [Reason:] A variable can’t start with a number. Also $this is a special variable that can’t be assigned, but $This can be assigned.

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

  1.     <?php 
  2.     $foo = 'Bob';              
  3.     $bar = &$foo;              
  4.     $bar = "My name is $bar";  
  5.     echo $bar;
  6.     echo $foo;
  7.     ?>

a) Error
b) My name is BobBob
c) My name is BobMy name is Bob
d) My name is Bob Bob

View Answer

Answer: c [Reason:] The $bar = &$foo; line will reference $foo via $bar.

3. Which of the following PHP statements will output Hello World on the screen?
i) echo (“Hello World”);
ii) print (“Hello World”);
iii) printf (“Hello World”);
iv) sprintf (“Hello World”);
a) i) and ii)
b) i), ii) and iii)
c) All of the mentioned
d) i), ii) and iv)

View Answer

Answer: b [Reason:] echo(), print() and printf() all three can be used to output a statement onto the screen. The sprintf() statement is functionally identical to printf() except that the output is assigned to a string rather than rendered to the browser.

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

  1.     <?php
  2.     $color = "maroon";
  3.     $var = $color[2];
  4.     echo "$var";
  5.     ?>

a) a
b) Error
c) $var
d) r

View Answer

Answer: d [Reason:] PHP treats strings in the same fashion as arrays, allowing for specific characters to be accessed via array offset notation.

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

  1.     <?php
  2.     $score = 1234;
  3.     $scoreboard = (array) $score;
  4.     echo $scoreboard[0];
  5.     ?>

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

View Answer

Answer: c [Reason:] The (array) is a cast operator which is used for converting values from other data types to array.

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

  1.     <?php
  2.     $total = "25 students";
  3.     $more = 10;
  4.     $total = $total + $more;
  5.     echo "$total";
  6.     ?>

a) Error
b) 35 students
c) 35
d) 25 students

View Answer

Answer: c [Reason:] The integer value at the beginning of the original $total string is used in the calculation. However if it begins with anything but a numerical value, the value will be 0.

7. Which of the below statements is equivalent to $add += $add ?
a) $add = $add
b) $add = $add +$add
c) $add = $add + 1
d) $add = $add + $add + 1

View Answer

Answer: b [Reason:] a += b is an addition assignment whose outcome is a = a + b. Same can be done with subtraction,multiplication,division etc.

8. Which statement will output $x on the screen?
a) echo “$x”;
b) echo “$$x”;
c) echo “/$x”;
d) echo “$x;”;

View Answer

Answer: a [Reason:] A backslash is used so that the dollar sign is treated as a normal string character rather than prompt PHP to treat $x as a variable. The backslash used in this manner is known as escape character.

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

  1.     <?php
  2.     function track() {
  3.     static $count = 0;
  4.     $count++;
  5.     echo $count;
  6.     }
  7.     track();
  8.     track();
  9.     track();
  10.     ?>

a) 123
b) 111
c) 000
d) 011

View Answer

Answer: a [Reason:] Because $count is static, it retains its previous value each time the function is executed.

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

  1.     <?php
  2.     $a = "clue";
  3.     $a .= "get";
  4.     echo "$a";
  5.     ?>

a) get
b) true
c) false
d) clueget

View Answer

Answer: d [Reason:] .= is a concatenation-assignment. $a equals its current value concatenated with “get”.

PHP MCQ Set 3

1. How many functions does PHP offer for searching and modifying strings using Perl-compatible regular expressions.
a) 7
b) 8
c) 9
d) 10

View Answer

Answer: b [Reason:] The functions are preg_filter(), preg_grep(), preg_match(), preg_match_all(), preg_quote(), preg_replace(), preg_replace_callback(), and preg_split().

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

  1.     <?php
  2.     $foods = array("pasta", "steak", "fish", "potatoes");
  3.     $food = preg_grep("/^s/", $foods);
  4.     print_r($food);
  5.     ?>

a) Array ( [0] => pasta [1] => steak [2] => fish [3] => potatoes )
b) Array ( [3] => potatoes )
c) Array ( [1] => steak )
d) Array ( [0] => potatoes )

View Answer

Answer: c [Reason:] This function is used to search an array for foods beginning with s.

3. Say we have two compare two strings which of the following function/functions can you use?
i) strcmp()
ii) strcasecmp()
iii) strspn()
iv) strcspn()
a) i) and ii)
b) iii) and iv)
c) None of the mentioned
d) All of the mentioned

View Answer

Answer: d [Reason:] All of the functions mentioned above can be used to compare strings in some or the other way.

4. Which one of the following functions will convert a string to all uppercase?
a) strtoupper()
b) uppercase()
c) str_uppercase()
d) struppercase()

View Answer

Answer: a [Reason:] Its prototype follows string strtoupper(string str).

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

  1.     <?php
  2.     $title = "O'malley wins the heavyweight championship!";
  3.     echo ucwords($title);
  4.     ?>

a) O’Malley Wins The Heavyweight Championship!
b) O’malley Wins The Heavyweight Championship!
c) O’Malley wins the heavyweight championship!
d) o’malley wins the heavyweight championship!

View Answer

Answer: d [Reason:] The ucwords() function capitalizes the first letter of each word in a string. Its prototype follows: string ucwords(string str).

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

  1.     <?php
  2.     echo str_pad("Salad", 5)." is good.";
  3.     ?>

a) SaladSaladSaladSaladSalad is good
b) is good SaladSaladSaladSaladSalad
c) is good Salad
d) Salad is good

View Answer

Answer: d [Reason:] The str_pad() function pads a string with a specified number of characters.

7. Which one of the following functions can be used to concatenate array elements to form a single delimited string?
a) explode()
b) implode()
c) concat()
d) concatenate()

View Answer

Answer: b [Reason:] None.

8. Which one of the following functions finds the last occurrence of a string, returning its numerical position?
a) strlastpos()
b) strpos()
c) strlast()
d) strrpos()

View Answer

Answer: d [Reason:] None.

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

  1.     <?php
  2.     $author = "";
  3.     $author = str_replace("a","@",$author);
  4.     echo "Contact the author of this article at $author.";
  5.     ?>

a) Contact the author of this article at @mple.com
b) the @uthor of this @rticle @t @
c) Contact the author of this article at @
d) Error

View Answer

Answer: c [Reason:] The str_replace() function case sensitively replaces all instances of a string with another.

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

  1.     <?php
  2.     $url = "";
  3.     echo ltrim(strstr($url, "@"),"@");
  4.     ?>

a)
b) nachiketh
c)
d) example.com

View Answer

Answer: d [Reason:] The strstr() function returns the remainder of a string beginning with the first occurrence of a predefined string.

PHP MCQ Set 4

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

  1.     <?php
  2.     $fruits = array ("mango", "apple", "pear", "peach");
  3.     $fruits = array_flip($fruits);
  4.     echo ($fruits[0]);
  5.     ?>

a) mango
b) error
c) peach
d) 0

View Answer

Answer: b [Reason:] As we are flipping the values, $fruits[“mango”] = 0, $fruits[“apple”] = 1 and so on.

2. Which of the functions is used to sort an array in descending order?
a) sort()
b) asort()
c) rsort()
d) dsort()

View Answer

Answer: c [Reason:] sort() function is used to sort in ascending order where as rsort() meaning reverse sort is used for sorting in descending order.

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

  1.     <?php
  2.     $fruits = array ("mango", "apple", "peach", "pear");
  3.     $fruits = asort ($fruits);
  4.     printr ($fruits);
  5.     ?>

a) Array ( [1] => apple [0] => mango [2] => peach [3] => pear )
b) Array ( [0] => apple [1] => mango [2] => peach [3] => pear )
c) Error
d) Array ( [1] => apple [0] => mango [3] => peach [2] => pear )

View Answer

Answer: a [Reason:] The function asort() sorts the array in ascending order, except that the key/value corresponding is maintained.

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

  1.     <?php
  2.     $arr = array ("picture1.JPG", "picture2.jpg",
  3.     "Picture10.jpg", "picture20.jpg");
  4.     sort($arr);
  5.     print_r($arr);
  6.     ?>

a) Array ( [0] => picture1.JPG [1] => Picture10.jpg [2] => picture2.jpg [3] => picture20.jpg )
b) Array ( [0] => picture1.JPG [1] => picture2.jpg [2] => Picture10.jpg [3] => picture20.jpg )
c) Array ( [0] => Picture10.jpg [1] => picture1.JPG [2] => picture2.jpg [3] => picture20.jpg )
d) Array ( [0] => Picture10.jpg [1] => picture1.JPG [2] => picture20.jpg [3] => picture2.jpg )

View Answer

Answer: c [Reason:] While sorting each character is compared with the others and sorted using ascii values therefore we the sorted array to be like option c.

5. Say in the above question you need to get the array sorted in the manner we humans would have done it i.e picture1 then picture2 etc.. Which of the following function should be used?
a) dsort()
b) casesort()
c) natcasesort()
d) naturalsort()

View Answer

Answer: c [Reason:] None.

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

  1.     <?php
  2.     $face = array ("A", "J", "Q", "K");
  3.     $number = array ("2","3","4", "5", "6", "7", "8", "9", "10");
  4.     $cards = array_merge ($face, $number);
  5.     print_r ($cards);
  6.     ?>

a) Array ( [0] => A [1] => J [2] => Q [3] => K [4] => 2 [5] => 3 [6] => 4 [7] => 5 [8] => 6 [9] => 7 [10] => 8 [11] => 9 [12] => 10 )
b) Array ( [0] => A [1] => 2 [2] => J [3] => 3 [4] => Q [5] => 4 [6] => K [7] => 5 [8] => 6 [9] => 7 [10] => 8 [11] => 9 [12] => 10 )
c) Error
d) Array ( [0] => 2 [1] => 3 [2] => 4 [3] => 5 [4] => 6 [5] => 7 [6] => 8 [7] => 9 [8] => 10 [9] => A [10] => J [11] => Q [12] => K )

View Answer

Answer: a [Reason:] The resulting array will begin with the first input array parameter, appending each subsequent array parameter in the order of appearance.

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

  1.     <?php
  2.     $fruits = array ("apple", "mango", "peach", "pear",
  3.     "orange");
  4.     $subset = array_slice ($fruits, 2);
  5.     print_r ($subset);
  6.     ?>

a) Array ( [0] => peach )
b) Array ( [0] => apple [1] => mango [2] => peach )
c) Array ( [0] => apple [1] => mango )
d) Array ( [0] => peach [1] => pear [2] => orange )

View Answer

Answer: d [Reason:] The array_slice() function returns a section of an array based on a starting and ending offset value.

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

  1.     <?php
  2.     $fruits = array ("apple", "mango", "peach", "pear",
  3.     "orange");
  4.     $subset = array_splice ($fruits, 2);
  5.     print_r ($fruits);
  6.     ?>

a) Error
b) Array ( [0] => apple [1] => mango [2] => peach )
c) Array ( [0] => apple [1] => mango )
d) Array ( [0] => pear [1] => orange )

View Answer

Answer: c [Reason:] The array_splice() function removes all elements of an array found within a specified range.

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

  1.     <?php
  2.     $number = array ("4", "hello", 2);
  3.     echo (array_sum ($number));
  4.     ?>

a) 4hello2
b) 4
c) 2
d) 6

View Answer

Answer: d [Reason:] The array_sum() function add all the values of the input array together, returning the final sum. If a string datatype is found, it’ll be ignored.

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

  1.     <?php
  2.     $array1 = array ("KA", "LA", "CA", "MA", "TA");
  3.     $array2 = array ("KA", "IA", "CA", "GA", "TA");
  4.     $inter = array_intersect ($array1, $array2);
  5.     print_r ($inter);
  6.     ?>

a) Array ( [0] => KA [1] => LA [2] => CA [3] => MA [4] => TA [5] => IA [6] => GA )
b) Array ( [0] => KA [2] => CA [4] => TA )
c) Array ( [1] => IA [3] => GA )
d) Array ( [1] => LA [3] => MA )

View Answer

Answer: b [Reason:] The array_intersect() function returns a key preserved array consisting only of those values present in the first array that are also present in each of the other input arrays.

PHP MCQ Set 5

1. Which version of PHP introduced class type hints?
a) PHP 4
b) PHP 4.3
c) PHP 5
d) PHP 5.3

View Answer

Answer: c [Reason:] None.

2. Inheritance is the means by which one or more classes can be derived from a/an ___ class.
a) base
b) abstract
c) null
d) predefined

View Answer

Answer: a [Reason:] A class that inherits from another is said to be a subclass of it. This relationship is often described in terms of parents and children. A child class is derived from and inherits characteristics from the parent.

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

  1. <?php
  2. class MyClass
  3. {
  4. }
  5.  
  6. class NotMyClass
  7. {
  8. }
  9. $a = new MyClass;
  10.  
  11. var_dump($a instanceof MyClass);
  12. var_dump($a instanceof NotMyClass);
  13. ?>

a) bool(true)
bool(true)
b) bool(false)
bool(false)
c) bool(true)
bool(false)
d) bool(false)
bool(true)

View Answer

Answer: c [Reason:] instanceof is used to determine whether a PHP variable is an instantiated object of a certain class.

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

  1. <?php
  2. class ParentClass
  3. {
  4. }
  5.  
  6. class MyClass extends ParentClass
  7. {
  8. }
  9.  
  10. $a = new MyClass;
  11.  
  12. var_dump($a instanceof MyClass);
  13. var_dump($a instanceof ParentClass);
  14. ?>

a) bool(false)
bool(false)
b) bool(true)
bool(true)
c) bool(false)
bool(true)
d) bool(true)
bool(false)

View Answer

Answer: b [Reason:] instanceof can also be used to determine whether a variable is an instantiated object of a class that inherits from a parent class.

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

  1. <?php
  2. class MyClass
  3. {
  4. }
  5.  
  6. $a = new MyClass;
  7. var_dump(!($a instanceof stdClass));
  8. ?>

a) bool(true)
b) bool(false)
c) error
d) none of the above

View Answer

Answer: a [Reason:] To check if an object is not an instanceof a class, the logical not operator can be used.

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

  1. <?php
  2. interface MyInterface
  3. {
  4. }
  5.  
  6. class MyClass implements MyInterface
  7. {
  8. }
  9.  
  10. $a = new MyClass;
  11.  
  12. var_dump($a instanceof MyClass);
  13. var_dump($a instanceof MyInterface);
  14. ?>

a) bool(false)
bool(false)
b) bool(true)
bool(true)
c) bool(false)
bool(true)
d) bool(true)
bool(false)

View Answer

Answer: b [Reason:] instanceof can also be used to determine whether a variable is an instantiated object of a class that implements an interface.

7. What should be used to refer to a method in the context of a class rather than an object you use?
a) ->
b) __
c) $
d) ::

View Answer

Answer: d [Reason:] Example- parent::__construct()

8. Prior to which version of PHP did constructors took the name of the enclosing class.
a) PHP 4
b) PHP 5
c) PHP 5.3
d) PHP 5.4

View Answer

Answer: b [Reason:] The new unified constructors use the name __construct(). Using the old syntax, a call to a parent constructor would tie you to that particular class: parent::ShopProduct();

9. Which method or property can only be accessed from within the enclosing class? Even subclasses have no access.
a) public
b) friendly
c) private
d) protected

View Answer

Answer: c [Reason:] None.

10. A mutator method is also called as..
a) Setter
b) Accessor
c) Getter
d) Destructor

View Answer

Answer: a [Reason:] An accessor method is called getter. The common use of a mutator method is to initialise the value of member variables of a class.

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

.woocommerce-message { background-color: #98C391 !important; }