PHP Questions & Answers – Basics of Object-Oriented PHP- 2

This set of PHP Questions and Answers for Experienced people focuses 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
View Answer

Answer: c
Explanation: 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)
View Answer

Answer: c
Explanation: 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()
View Answer

Answer: d
Explanation: 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
View answer

Answer: b
Explanation: 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()
View Answer

Answer: c
Explanation: 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()
View Answer

Answer: c
Explanation: 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
View Answer

Answer: a
Explanation: 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.     <?php
  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()
View Answer

Answer: b
Explanation: 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
View Answer

Answer: d
Explanation: 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
View Answer

Answer: c
Explanation: 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.

Sanfoundry Global Education & Learning Series – PHP Programming.

To practice all areas of PHP for Experienced people, here is complete set of 1000+ Multiple Choice Questions and Answers on PHP.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.