This set of PHP Multiple Choice Questions & Answers (MCQs) 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
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
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
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
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
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
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
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?
<?php
class Example
{
public $name;
function Sample()
{
echo "This is an example";
}
}
?>
a) echo “This is an example”;
b) public $name;
c) class Example
d) function sample()
View Answer
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
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
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 questions on PHP Programming, here is complete set of 1000+ Multiple Choice Questions and Answers on PHP.
- Check Information Technology Books
- Apply for Programming Internship
- Check MCA Books
- Practice MCA MCQs
- Practice Programming MCQs