PHP MCQ Number 01299

PHP MCQ Set 1

1. Which version of PHP introduced the static keyword?
a) PHP 4
b) PHP 5
c) PHP 5.2
d) PHP 5.3

Answer

Answer: b [Reason:] The static keyword was introduced with PHP 5. It cannot be used in PHP 4 scripts.

2. Which keyword is used to access a static method or property from within the same class(rather than from child)?
a) static
b) strat
c) self
d) set

Answer

Answer: c [Reason:] Self is to classes what the $this pseudo-variable is to objects.

3. In which of the following circumstance should you use a static reference to a non static method?
a) Making a method call using parent
b) Making a method call using child
c) Making an object call using parent
d) Making an object call using child

Answer

Answer: a [Reason:] Making a method call using parent is the only circumstance in which you should use a static reference to a non-static method.

4. Which one of the following variable cannot be used inside a static method?
a) $this
b) $get
c) $set
d) $date

Answer

Answer: a [Reason:] By definition, static methods are not invoked in the context of an object. For this reason, static methods and properties are often referred to as class variables and properties.

5. What does PDO stand for?
a) PHP Data Orientation
b) PHP Database Object
c) PHP Database Orientation
d) PHP Data Object

Answer

Answer: d [Reason:] The PDO class provides a common interface to different database applications.

6. Which version of PHP allows you to define constant properties within a class?
a) PHP 4
b) PHP 4.1
c) PHP 4.3
d) PHP 5

Answer

Answer: d [Reason:] None.

7. Which keyword is used to declare a constant property?
a) const
b) con
c) constant
d) _constant

Answer

Answer: a [Reason:] A constant property is declared with the const keyword. Like global constants, class constants cannot be changed once they are set.

8. Which one of the following is a constant variable?
a) const $name
b) const $NAME
c) constant NAME
d) const NAME

Answer

Answer: d [Reason:] Constants are not prefixed with a dollar sign like regular properties. By convention, they are often named using only uppercase characters

9. What will happen if you try to set a value to a constant once it has been declared?
a) The value of the variable will change
b) The value of the variable will not change
c) Parse Error
d) Nothing

Answer

Answer: c [Reason:] You should use constants when your property needs to be available across all instances of a class, and when the property value needs to be fixed and unchanging.

10. How many of the following can be contained in constants?
i) boolean
ii) integer
iii) float
iv) string
a) 1
b) 2
c) 3
d) 4

Answer

Answer: d [Reason:] All scalar data i.e boolean, integer, float and string can be contained in constants.

PHP MCQ Set 2

1. Which one of the following method is invoked when a value is assigned to an undefined property?
a) __get()
b) __set()
c) __isset()
d) __call()

Answer

Answer: b [Reason:] The __set() method is invoked when client code attempts to assign to an undefined property. It is passed two arguments: the name of the property, and the value the client is attempting to set.

2. Which one of the following method is invoked when an undefined method is called by client code?
a) __get()
b) __isset()
c) __unset()
d) __call()

Answer

Answer: d [Reason:] The __call() method is probably the most useful of all the interceptor methods. The __call() method can be useful for delegation. Delegation is the mechanism by which one object passes method invocations on to a second.

3. Which method introduced in PHP 5, is invoked just before an object is garbage collected?
a) __collect()
b) __garbage()
c) __destruct()
d) __destructor()

Answer

Answer: c [Reason:] You can use this method to perform any final cleaning up that might be necessary. Imagine, for example, a class that saves itself to a database when so ordered. I could use the __destruct() method to ensure that an instance saves its data when it is deleted.

4. Which one of the following statements is true ?

  1. class CopyMe {}
  2. $first = new CopyMe();
  3. $second = $first;

a) In PHP 4: $second and $first are 2 distinct objects
b) In PHP 5: $second and $first are 2 distinct objects
c) In PHP 4: $second and $first refer to one object
d) None of the mentioned

Answer

Answer: a [Reason:] None.

5. Which keyword must be added before $first variable on the third line of the above question to make $second and $first as distinct objects in PHP 5?
a) copy
b) clone
c) cut
d) Can’t add any word to make them distinct

Answer

Answer: b [Reason:] Clone operates on an object instance, producing a by-value copy.

6. What will be output of following code ?

  1. class StringThing {}
  2. $st = new StringThing();
  3. print $st;

Before the version PHP 5.2
a) Object Not Found
b) Object id #1
c) PHP Catchable fatal error
d) Cannot initialize object

Answer

Answer: b [Reason:] Since PHP 5.2, this code will produce an error like this: PHP Catchable fatal error: Object of class StringThing could not be converted to string.

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

  1. class Person 
  2. {
  3.     function getName() { return "Bob"; }
  4.     function getAge() { return 44; }
  5.     function __toString() {
  6.         $desc = $this->getName();
  7.         $desc .= " (age ".$this->getAge().")";
  8.         return $desc;
  9.     }
  10. }
  11. $person = new Person();
  12. print $person;

a) Object Not Found
b) PHP Catchable fatal error
c) BOB (age 44)
d) BOB

Answer

Answer: c [Reason:] By implementing a __toString() method, you can control how your objects represent themselves when printed. The method is invoked automatically when your object is passed to print or echo, and its return value is substituted.

8. __clone() is run on the ___ object.
a) original
b) pseudo
c) external
d) copied

Answer

Answer: d [Reason:] __clone() is run on the copied object and not the original.

9. Which method is invoked when an undefined property is accessed ?
a) __get()
b) __isset()
c) __unset()
d) __undefined()

Answer

Answer: a [Reason:] None.

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

  1. class Checkout
  2.  {
  3.     final function totalize() 
  4.     {
  5.         // calculate bill
  6.     }
  7.  }
  8.  
  9. class IllegalCheckout extends Checkout 
  10. {
  11.     final function totalize() 
  12.     {
  13.         // change bill calculation
  14.     }
  15. }

a) PHP Fatal error: Class IllegalCheckout may not inherit from final class
b) Value of the bill calculated
c) PHP Fatal error: Cannot find object
d) PHP Fatal error: Cannot override final method

Answer

Answer: d [Reason:] A final class cannot be subclassed. Less drastically, a final method cannot be overridden.

PHP MCQ Set 3

1. Object-oriented code tries to minimize dependencies by moving responsibility for handling tasks away from ___ and toward the objects in the system.
a) server code
b) client code
c) machine code
d) procedural code

Answer

Answer: b [Reason:] Procedural code takes the form of a sequential series of commands and method calls. The controlling code tends to take responsibility for handling differing conditions. This top-down control can result in the development of duplications and dependencies across a project. Object-oriented code tries to minimize these dependencies by moving responsibility for handling tasks away from client code and toward the objects in the system.

2. Placing a static method for generating ___ objects in the ___ class is convenient.
a) child parent
b) parent child
c) final static
d) static final

Answer

Answer: a [Reason:] Such a design decision does have its own consequences, however.

3. The extent to which proximate procedures are related to one another is known as…
a) Coupling
b) Balancing
c) Cohesion
d) Co-relation

Answer

Answer: c [Reason:] Ideally, you should create components that share a clear responsibility. If your code spreads related routines widely, you will find them harder to maintain as you have to hunt around to make changes. Our ParamHandler classes collect related procedures into a common context.

4. ______ occurs when discrete parts of a system’s code are tightly bound up with one another so that a change in one part necessitates changes in the others.
a) Loose Coupling
b) Tight Coupling
c) Co-relation
d) Balancing

Answer

Answer: b [Reason:] Tight coupling is by no means unique to procedural code, though the sequential nature of such code makes it prone to the problem.

5. ________ code makes change easier because the impact of altering an implementation will be localized to the component being altered.
a) Orthogonal
b) Cohesion
c) Coupling
d) Balancing

Answer

Answer: a [Reason:] Orthogonality, it is argued, promotes reuse in that components can be plugged into new systems without needing any special configuration. Such components will have clear inputs and outputs independent of any wider context.

6. Polymorphism is also known as______
a) switch
b) overact
c) encapsulation
d) class switching

Answer

Answer: d [Reason:] Polymorphism is the maintenance of multiple implementations behind a common interface.

7. Which one of the following is know as the key to object-oriented programming ?
a) Polymorphism
b) Encapsulation
c) Data Abstraction
d) Orthogonality

Answer

Answer: b [Reason:] Encapsulation simply means the hiding of data and functionality from a client.

8. Which one among the following means tight coupling ?
a) Code Duplication
b) Inheritance
c) Encapsulation
d) Polymorphism

Answer

Answer: a [Reason:] Duplication is one of the great evils in code. Take a look at the instances of repetition in your system. Perhaps they belong together. If you change something fundamental about one routine, will the similar routines need amendment? If this is the case, they probably belong in the same class.

9. UML stands for?
a) unified mailing language
b) unified modeling logo
c) undefined modeling language
d) unified modeling language

Answer

Answer: d [Reason:] The UML emerged as a standard only after long years of intellectual and bureaucratic sparring among the great and good of the object oriented design community.

10. In a class diagram the class is divided into three sections, what is displayed in the first section?
a) Class Attributes
b) Class Declaration
c) Class Name
d) Class Functions

Answer

Answer: c [Reason:] The other two sections are optional when no more information than the class name is known.

PHP MCQ Set 4

1. Which one of the following is not a valid class name?
a) ShopProduct
b) Shopproduct
c) Shopproduct1
d) 1shopproduct

Answer

Answer: d [Reason:] You declare a class with the class keyword and an arbitrary class name. Class names can be any combination of numbers and letters, although they must not begin with a number.

2. Fill in the blank with the best option. An Object is a/an ________ of a class.
a) type
b) prototype
c) instance
d) object

Answer

Answer: c [Reason:] An object is said to be an instance of its class. It is of the type defined by the class.

3. There are two objects-
$product1 = new Shop();
$product2 = new Shop();
Which one of the following statements is right about them?
a) $product1 and $product2 are same objects of the same type generated from a single class.
b) $product1 and $product2 are different objects of the same type generated from a single class.
c) $product1 and $product2 are same objects of the different type generated from a single class.
d) $product1 and $product2 are different objects of the different type generated from a single class.

Answer

Answer: b [Reason:] None.

4. Which version of PHP introduced the visibility keywords i.e public, private, and protected?
a) PHP 4
b) PHP 5
c) PHP 5.1
d) PHP 5.3

Answer

Answer: b [Reason:] In PHP 4, all properties were declared with var keyword, which is identical in effect to using public. For the sake of backward compatibility, PHP 5 accepts var in place of public for properties.

5. Which characters is used to access property variables on an object-by-object basis?
a) ::
b) =
c) ->
d) .

Answer

Answer: c [Reason:] Example: $product1->title=”My Life”;

6. Code that uses a class, function, or method is often described as the..
a) client code
b) user code
c) object code
d) class code

Answer

Answer: a [Reason:] Code that uses a class, function, or method is often described as the class’s, function’s, or method’s client or as client code.

7. Which keyword precedes a method name?
a) method
b) function
c) public
d) protected

Answer

Answer: b [Reason:] A method declaration resembles a function declaration. The function keyword precedes a method name, followed by an optional list of argument variables in parentheses.

8. If you omit the visibility keyword in your method declaration, by default the method will be declared as..
a) public
b) private
c) protected
d) friendly

Answer

Answer: a [Reason:] By declaring a method public, you ensure that it can be invoked from outside of the current object.

9. Which function is used to determine whether the variable’s value is either TRUE or FALSE?
a) boolean()
b) is_boolean()
c) bool()
d) is_bool()

Answer

Answer: d [Reason:] None.

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

  1. <?php
  2.     class ShopProductWriter
  3.     {
  4.         public function write( $shopProduct )
  5.         {
  6.             $str = "{$shopProduct->title}: " .$shopProduct->getProducer() ." ({$shopProduct->price})n";
  7.             print $str;
  8.         }
  9.     }
  10.     $product1 = new ShopProduct( "My Antonia", "Willa", "Cather", 5.99 );
  11.     $writer = new ShopProductWriter();
  12.     $writer->write( $product1 );
  13. ?>

a) Error
b) Cather: Willa My Antonia (5.99)
c) Willa: Cather My Antonia (5.99)
d) My Antonia: Willa Cather (5.99)

Answer

Answer: d [Reason:] None.

PHP MCQ Set 5

1. A package is a set of related _________
a) Objects
b) Classes
c) Programs
d) Functions

Answer

Answer: b [Reason:] A package is a set of related classes, usually grouped together in some way. Packages can be used to separate parts of a system from one another.

2. Till which version of PHP, developers were forced to name their files in a global context?
a) PHP 4
b) PHP 5
c) PHP 5.2
d) PHP 5.3

Answer

Answer: d [Reason:] If you named a class ShoppingBasket, it would become instantly available across your system.

3. Which of the following can you place inside a namespace?
i) classes
ii) functions
iii) variables
a) i)
b) ii)
c) iii)
d) All of the mentioned

Answer

Answer: d [Reason:] A namespace is a bucket in which you can place your classes, functions and variables.

4. Which one of the following is the correct way of declaring a namespace?
a) namespace my;
b) namespace my();
c) my namespace;
d) namespace(my);

Answer

Answer: a [Reason:] The namespace declaration must be the first statement in its file.

5. Which symbol is used to declare nested namespaces?
a) /
b)
c) .
d) |

Answer

Answer: b [Reason:] Example – namespace comgetinstanceutil;

6. Output of
namespace main;
comgetinstanceutilDebug::helloWorld() is PHP Fatal error: Class ‘maincomgetinstanceutilDebug’ not found in …
Using which one of the following lines will the error be removed?
a) comgetinstanceutilDebug::helloWorld();
b) getinstanceutilDebug::helloWorld();
c) main.comgetinstanceutilDebug::helloWorld();
d) comgetinstanceutilDebug::helloWorld();

Answer

Answer: d [Reason:] PHP is looking below the namespace main for comgetinstanceutil and not finding it. That’s because we are using a relative namespace here. Just as you can make absolute URLs and filepaths by starting off with a separator so you can with namespaces.

7. Which keyword can be used to fix the above error?
a) fix
b) join
c) use
d) namespace

Answer

Answer: c [Reason:] Use keyword allows you to alias other namespaces within the current namespace. Example – namespace main;
use comgetinstanceutil;
utilDebug::helloWorld();

8. If I already had a Debug class in the main namespace. What will be the output of the following code?

  1. namespace main;
  2. use comgetinstanceutilDebug;
  3. <pre lang="php" cssfile="hk1_style" >
  4. class Debug {
  5.     static function helloWorld() {
  6.         print "hello from mainDebug";
  7.     } 
  8. }
  9.  
  10. Debug::helloWorld();

a) error
b) hello from main
c) hello from mainDebug
d) debug

Answer

Answer: a [Reason:] PHP Fatal error: Cannot declare class mainDebug because the name is already in use.

9. Which one of the following statements is true for include_once() and require_once()?
a) Both are exactly the same
b) include_once is used for files where as require_once() is not
c) Both Handle the errors in the same way
d) Both do not handle the errors in the same way

Answer

Answer: d [Reason:] The only difference between the include() and require() statements lies in their handling of errors. A file invoked using require() will bring down your entire process when you meet an error. The same error encountered via a call to include() will merely generate a warning and end execution of the included file.

10. Which one of the following statements is true for require() and require_once()?
a) They are functions
b) They are statements
c) They’ll not work if the () is not present
d) They can not be used to require files

Answer

Answer: b [Reason:] require() and require_once() are actually statements, not functions. This means that you can omit the brackets when using them.

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

ed010d383e1f191bdb025d5985cc03fc?s=120&d=mm&r=g

DistPub Team

Distance Publisher (DistPub.com) provide project writing help from year 2007 and provide writing and editing help to hundreds student every year.