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. Which function was introduced to help automate the inclusion of class files?
a) __load()
b) __preload()
c) __autoload()
d) __inload()

View Answer

Answer: c [Reason:] When the PHP engine encounters an attempt to instantiate an unknown class, it invokes the __autoload() function, passing it the class name as a string. It is up to the implementer to define a strategy for locating and including the missing class file.

2. How many times can you define __autoload in a process?
a) once
b) twice
c) thrice
d) as many times as needed

View Answer

Answer: a [Reason:] __autoload is a powerful tool, but it does have some limitations. In particular, you can only define it once in a process. If you need to change your autoload function dynamically you should look at the spl_autoload_register function, which supports that functionality.

3. Which one of the following functions will you use to check that the class exists before you work with it?
a) class_exist()
b) class_exists()
c) exist()
d) exists_class()

View Answer

Answer: b [Reason:] The class_exists() function accepts a string representing the class to check for and returns a Boolean true value if the class exists and false otherwise.

4. Which one of the following will you use to check the class of an object?
a) class()
b) _class()
c) class_check()
d) get_class()

View Answer

Answer: d [Reason:] This function accepts any object as an argument and returns its class name as a string.

5. PHP 4 did not support instanceof. Instead, which function did it provide?
a) is()
b) get_class()
c) is_a()
d) is_the()

View Answer

Answer: c [Reason:] As of PHP 5.3 is_a() no longer deprecated.

6. You use the get_class_methods() function to return the names of all the methods in the class. Which function will you use to print it on the screen?
a) printf()
b) print_ar
c) print_r
d) echo

View Answer

Answer: c [Reason:] The function get_class_methods returns an array therefore we cannot use echo.

7. If you call a method and it doesn’t exist it’ll cause a problem. To check the method which function will you use?
a) _method()
b) methodexists()
c) is_callable()
d) is_method()

View Answer

Answer: c [Reason:] There is another method which you can use – method_exists() for similar purpose.

8. What will be the output if a protected method is given as argument to the function method_exist()?
a) Method does not exist
b) False
c) Error
d) True

View Answer

Answer: d [Reason:] method_exists() returns true for private and protected methods as well as for public ones. Remember that the fact that a method exists does not mean that it will be callable.

9. Which one of the following function should I use to find the parent class of a class?
a) get_parent_class()
b) parent_class()
c) class_parent()
d) get_class_parent()

View Answer

Answer: a [Reason:] The class functions also allow us to chart inheritance relationships.This function requires either an object or a class name, and it returns the name of the superclass, if any. If no such class exists, that is, if the class we are testing does not have a parent, then the function returns false.

10. Which class accepts a class name or an object reference and returns an array of interface name?
a) class_implements()
b) is_subclass()
c) is_subclass_of()
d) class_interface()

View Answer

Answer: a [Reason:] is_subclass_of() will tell you only about class inheritance relationships. It will not tell you that a class implements an interface.

PHP MCQ Set 2

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

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

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

View Answer

Answer: c [Reason:] The condition of the loop is always false 0.

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

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

a) infinite loop
b) 0 1 2 3 4 5
c) 0 1 0 1 2 0 1 2 3 0 1 2 3 4 0 1 2 3 4 5 0 1 2 3 4 5
d) no output

View Answer

Answer: a [Reason:] In the second loop j value is not being changed.

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

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

a) infinite loop
b) 0 1 2 3 4 5
c) 0 1 0 1 2 0 1 2 3 0 1 2 3 4 0 1 2 3 4 5 0 1 2 3 4 5
d) no output

View Answer

Answer: d [Reason:] The second loop does not execute as the check condition is always false.

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

  1. <?php
  2. $user = array("Ashley", "Bale", "Shrek", "Blank");
  3. for ($x = 0; $x < count($user); $x++) 
  4. {
  5.     if ($user[$x] == "Shrek") 
  6. 	    continue;
  7.     printf ($user[$x]); 
  8. }
  9. ?>

a) AshleyBaleBlank
b) AshleyBale
c) AshleyBaleShrek
d) No output

View Answer

Answer: a [Reason:] Only the Shrek is skipped due to the continue statement.

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

  1. <?php
  2. $user = array("Ashley", "Bale", "Shrek", "Blank");
  3.     for ($x=0; $x < count($user) - 1; $x++)	
  4.     {
  5.         if ($user[$x++] == "Shrek") 
  6. 		    continue;
  7.         printf ($user[$x]); 
  8.     }
  9. ?>

a) AshleyBaleBlank
b) Bale
c) AshleyShrek
d) BaleBlank

View Answer

Answer: a [Reason:] Only Bale is printed as $x++ is done before printing and then checked.

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

  1. <?php
  2. $user = array("Ashley", "Bale", "Shrek", "Blank");
  3. for ($x = 0; $x < count($user); $x) 
  4. {
  5.     if ($user[$x++] == "Shrek") 
  6. 	    continue;
  7.     printf ($user[$x]); 
  8. }
  9. ?>

a) AshleyBaleBlank
b) BaleShrek
c) AshleyBlank
d) Bale

View Answer

Answer: b [Reason:] x is incremented only inside loop i the if condition.

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

  1. <?php
  2. for ($i = 0; $i % ++$i; $i++) 
  3. {
  4.     print"i";
  5. }
  6. ?>

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

View Answer

Answer: b [Reason:] Loop condition is true as i%(i+1) is a float non zero value in php.

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

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

a) iiiii
b) infinite loop
c) iiiiiiiiiiiiiiiiiiiiiiiii
d) no output

View Answer

Answer: a [Reason:] The i value is changed in the inner loop and reaches five, thus does not execute the second outer loop.

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

  1. <?php
  2. $a = array("hi", "hello", "bye");
  3. foreach ($a as $value) 
  4. {
  5.     if (count($a) == 2)
  6. 	    print $value;         
  7. }
  8. ?>

a) hihellobye
b) infinite loop
c) hihello
d) no output

View Answer

Answer: d [Reason:] As count($a) returns 3 the condition is always false.

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

  1. <?php
  2. $a = array("hi", "hello", "bye");
  3. for (;count($a) < 5;) 
  4. {
  5.     if (count($a) == 3)
  6. 	    print $a;         
  7. }
  8. ?>

a) ArrayArrayArrayArrayArrayArray….infinitely
b) (“hi”,”hello”,”bye”)(“hi”,”hello”,”bye”)(“hi”,”hello”,”bye”)(“hi”,”hello”,”bye”)…infinitely
c) hihellobyehihellobyehihellobyehihellobyehihellobyehihellobye…..infinitely
d) no output

View Answer

Answer: a [Reason:] As count($a) returns 3 the condition is always true, thus it prints $a, which returns its data type.

PHP MCQ Set 3

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

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

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

View Answer

Answer: b [Reason:] While condition always gives 1.

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

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

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

View Answer

Answer: b [Reason:] While condition always gives 1.

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

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

a) 4hello4hello4hello4hello4hello…..infinite
b) 5hello5hello5hello5hello5hello…..infinite
c) no output
d) error

View Answer

Answer: a [Reason:] i is decremented in the first while execution and then continuously incremented back.

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

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

a) 5
b) 555555555…infinitely
c) 54321
d) error

View Answer

Answer: b [Reason:] As it is && operator it is being incremented and decremented continuously.

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

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

a) 54321111111….infinitely
b) 555555555…infinitely
c) 54321
d) 5

View Answer

Answer: a [Reason:] As it is || operator the second expression is not evaluated till i becomes 1 then it goes into a loop.

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

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

a) 1234567891011121314….infinitely
b) 01234567891011121314…infinitely
c) 1
d) 0

View Answer

Answer: a [Reason:] As it is || operator the second expression is not evaluated and i is always incremented, in the first case to 1.

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

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

a) 1234567891011121314….infinitely
b) 01234567891011121314…infinitely
c) no output
d) error

View Answer

Answer: c [Reason:] The first conditio itsef fails thus the loop exits.

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

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

a) 00000000000000000000….infinitely
b) -1-1-1-1-1-1-1-1-1-1…infinitely
c) no output
d) error

View Answer

Answer: a [Reason:] (–$i > ++$i) evaluates to 0 but -1 makes it enters the loop and prints i which is 0.

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

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

a) 210
b) 10
c) no output
d) infinite loop

View Answer

Answer: a [Reason:] The loop ends when i becomes 0.

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

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

a) 210
b) 10
c) no output
d) infinite loop

View Answer

Answer: d [Reason:] The loop never ends as i is always incremented and then decremented.

PHP MCQ Set 4

1. Which of the following is/are an external data?
i) Cookies
ii) Input data from a form
iii) Server Variables
iv) Web services data
a) Only ii)
b) ii) and iii)
c) None of the mentioned
d) All of the mentioned

View Answer

Answer: d [Reason:] None.

2. How many types of filtering are present in PHP?
a) 3
b) 2
c) 4
d) None

View Answer

Answer: b [Reason:] There are two main types of filtering: validation and sanitization.

3. Which one of the following filter is used to filter several variables with the same or different filters?
a) filter_var_array()
b) filter_var()
c) filter_input
d) filter_input_array

View Answer

Answer: a [Reason:] None.

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

  1.     <?php
  2.     $num = "123";
  3.     if (!filter_var($num, FILTER_VALIDATE_INT))
  4.         echo("Integer is not valid");
  5.     else
  6.         echo("Integer is valid");
  7.     ?>

a) No output is returned
b) Integer is not valid
c) Integer is valid
d) Error

View Answer

Answer: c [Reason:] filter_var() – Filters a single variable with a specified filter.

5. Which one of the following does not describe a validating filter?
a) Are used to allow or disallow specified characters in a string
b) Are used to validate user input
c) Strict format rules
d) Returns the expected type on success or FALSE on failure

View Answer

Answer: a [Reason:] Option a) describes Sanitizing filters.

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

  1.     <?php
  2.     $var=300;
  3.     $int_options = array("options"=>array ("min_range"=>0, "max_range"=>256));
  4.     if (!filter_var($var, FILTER_VALIDATE_INT, $int_options))
  5.         echo("Integer is not valid");
  6.     else
  7.         echo("Integer is valid");
  8.     ?>

a) No output is returned
b) Integer is not valid
c) Integer is valid
d) Error

View Answer

Answer: b [Reason:] Since the integer is “300” it is not in the specified range, and the output of the code above will be: “Integer is not valid”.

8. Which one of the following filter checks if variable of specified type exists?
a) filter_has_var
b) filter_var
c) filter_id
d) filter_var_array

View Answer

Answer: a [Reason:] None.

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

  1.     <?php
  2.     $value = 'car';
  3.     $result = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
  4.     ?>

a) FALSE
b) TRUE
c) NULL
d) ERROR

View Answer

Answer: c [Reason:] There is an undocumented filter flag for FILTER_VALIDATE_BOOLEAN. The documentation implies that it will return NULL if the value doesn’t match the allowed true/false values. However this doesn’t happen unless you give it the FILTER_NULL_ON_FAILURE flag.

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

  1.     <?php
  2.     function convertSpace($string)
  3.     {
  4.         return str_replace("_", " ", $string);
  5.     }
  6.     $string = "Peter_is_a_great_guy!";
  7.     echo filter_var($string, FILTER_CALLBACK, array("options"=>"convertSpace"));
  8.     ?>

a) Peter_is_a_great_guy!
b) Peterisagreatguy!
c) Peter is a great guy!
d) Error

View Answer

Answer: c [Reason:] The code above converts all “_” to white spaces. Call the filter_var() function with the FILTER_CALLBACK filter and an array containing our function.

PHP MCQ Set 5

1. Which one of the following is the very first task executed by a session enabled page?
a) Delete the previous session
b) Start a new session
c) Check whether a valid session exists
d) Handle the session

View Answer

Answer: c [Reason:] None.

2. How many ways can a session data be stored?
a) 3
b) 4
c) 5
d) 6

View Answer

Answer: b [Reason:] Within flat files(files), within volatile memory(mm), using the SQLite database(sqlite), or through user defined functions(user).

3. Which directive determines how the session information will be stored?
a) save_data
b) session.save
c) session.save_data
d) session.save_handler

View Answer

Answer: d [Reason:] Its prototype follows: session.save_handler = files|mm|sqlite|user.

4. Which one of the following is the default PHP session name?
a) PHPSESSID
b) PHPSESID
c) PHPSESSIONID
d) PHPIDSESS

View Answer

Answer: a [Reason:] You can change this name by using the session.name directive.

5. If session.use_cookie is set to 0, this results in use of..
a) Session
b) Cookie
c) URL rewriting
d) Nothing happens

View Answer

Answer: c [Reason:] None.

6. If the directive session.cookie_lifetime is set to 3600, the cookie will live until..
a) 3600 sec
b) 3600 min
c) 3600 hrs
d) the browser is restarted

View Answer

Answer: a [Reason:] The lifetime is specified in seconds, so if the cookie should live 1 hour, this directive should be set to 3600.

7. Neglecting to set which of the following cookie will result in the cookie’s domain being set to the host name of the server which generated it.
a) session.domain
b) session.path
c) session.cookie_path
d) session.cookie_domain

View Answer

Answer: d [Reason:] The directive session.cookie_domain determines the domain for which the cookie is valid.

8. What is the default number of seconds that cached session pages are made available before the new pages are created?
a) 360
b) 180
c) 3600
d) 1800

View Answer

Answer: b [Reason:] The directive which determines this is session.cache_expire.

9. What is the default time(in seconds) for which session data is considered valid?
a) 1800
b) 3600
c) 1440
d) 1540

View Answer

Answer: c [Reason:] The session.gc_maxlifetime directive determines this duration. It can be set to any required value.

10. Which one of the following function is used to start a session?
a) start_session()
b) session_start()
c) session_begin()
d) begin_session()

View Answer

Answer: b [Reason:] None.

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