250+ TOP MCQs on PHP Basics of Object-Oriented PHP- 2 and Answers

PHP Questions and Answers for Experienced people on “Basics of Object-Oriented PHP- 2”.

1. Which method scope prevents a method from being overridden by a subclass?
a) Abstract
b) Protected
c) Final
d) Static
Answer: c
Clarification: When we declare a method is as final then it is not possible to override that method. Methods should not be overridden due to some security or any other reasons.

2. Which of the following statements is/are true about Constructors in PHP?

i) PHP 4 introduced class constructors.
ii) Constructors can accept parameters.
iii) Constructors can call class methods or other functions.
iv) Class constructors can call on other constructors.

a) ii)
b) ii) and iii)
c) i), ii), iii) and iv)
d) ii), iii) and iv)
Answer: c
Clarification: If a class name and the function name is similar then the function is known as constructor. Constructor is automatically called when an object will be initialized. Constructors can accept parameters. Constructors can call class methods or other functions. Class constructors can call on other constructors.

3. PHP recognizes constructors by the name _________
a) classname()
b) _construct()
c) function _construct()
d) function __construct()
Answer: d
Clarification: A double underscore followed by the construct keyword. Its syntax is function __construct ([ argument1, argument2,…..]) { Class Initialization code }.

4. Which version of PHP introduced the instanceof keyword?
a) PHP 4
b) PHP 5
c) PHP 5.3
d) PHP 6
Answer: b
Clarification: Using instanceof keyword we can determine whether an object is an instance of a class. $manager = new Employee() … if ($manager instanceof Employee) echo “True”;

5. Which one of the following functions is used to determine whether a class exists?
a) exist()
b) exist_class()
c) class_exist()
d) __exist()
Answer: c
Clarification: The class_exist() function returns true or false according to whether the class exists within the currently executing script content.

6. Which one of the following functions is used to determine object type?
a) obj_type()
b) type()
c) is_a()
d) is_obj()
Answer: c
Clarification: The is_a() function returns true if object belongs to a class type or if it belongs to a class that is a child of that class. Or else false is returned.

7. Which one of the following keyword is used to inherit our subclass into a superclass?
a) extends
b) implements
c) inherit
d) include
Answer: a
Clarification: When we extend a class then the subclass will inherit all the public and protected methods from the parent class.
The keyword implements are used with interfaces. With inheritance, we use the keyword extends.

8. In the following PHP code, what is/are the properties?

  1.     
  2.     class Example 
  3.     {
  4.         public $name;
  5.         function Sample()
  6.         {
  7.             echo "This is an example";
  8.         }
  9.     } 
  10.     ?>

a) echo “This is an example”;
b) public $name;
c) class Example
d) function sample()
Answer: b
Clarification: Above code is an example of ‘classes’ in PHP. Classes are the blueprints of objects. Classes are the programmer-defined data type, which includes the local methods and the local variables. Class is a collection of objects which has properties and behaviour.

9. Which keyword is used to refer to properties or methods within the class itself?
a) private
b) public
c) protected
d) $this
Answer: d
Clarification: In PHP, the self and ‘this’ keyword are used to refer the class members within the scope of a class itself. The class members can be either variables or functions.

10. Which keyword allows class members (methods and properties) to be used without needing to instantiate a new instance of the class?
a) protected
b) final
c) static
d) private
Answer: c
Clarification: Sometimes it is very handy to access the methods and properties in terms of a class rather than an object. But this can be done through static keyword. Any method declared as ‘static’ can be accessed without the creation of an object.

PHP for Experienced people,

Leave a Reply

Your email address will not be published. Required fields are marked *