This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “Object Basics-2”.
1. Which version of PHP introduced class type hints?
a) PHP 4
b) PHP 4.3
c) PHP 5
d) PHP 5.3
View Answer
Explanation: None.
2. Inheritance is the means by which one or more classes can be derived from a/an ___________ class.
a) base
b) abstract
c) null
d) predefined
View Answer
Explanation: A class that inherits from another is said to be a subclass of it. This relationship is often described in terms of parents and children. A child class is derived from and inherits characteristics from the parent.
3. What will be the output of the following PHP code?
<?php
class MyClass
{
}
class NotMyClass
{
}
$a = new MyClass;
var_dump($a instanceof MyClass);
var_dump($a instanceof NotMyClass);
?>
a)
bool(true) bool(true)
b)
bool(false) bool(false)
c)
bool(true) bool(false)
d)
bool(false) bool(true)View Answer
Explanation: instanceof is used to determine whether a PHP variable is an instantiated object of a certain class.
4. What will be the output of the following PHP code?
<?php
class ParentClass
{
}
class MyClass extends ParentClass
{
}
$a = new MyClass;
var_dump($a instanceof MyClass);
var_dump($a instanceof ParentClass);
?>
a)
bool(false) bool(false)
b)
bool(true) bool(true)
c)
bool(false) bool(true)
d)
bool(true) bool(false)View Answer
Explanation: instanceof can also be used to determine whether a variable is an instantiated object of a class that inherits from a parent class.
5. What will be the output of the following PHP code?
<?php
class MyClass
{
}
$a = new MyClass;
var_dump(!($a instanceof stdClass));
?>
a) bool(true)
b) bool(false)
c) error
d) none of the mentioned
View Answer
Explanation: To check if an object is not an instanceof a class, the logical not operator can be used.
6. What will be the output of the following PHP code?
<?php
interface MyInterface
{
}
class MyClass implements MyInterface
{
}
$a = new MyClass;
var_dump($a instanceof MyClass);
var_dump($a instanceof MyInterface);
?>
a)
bool(false) bool(false)
b)
bool(true) bool(true)
c)
bool(false) bool(true)
d)
bool(true) bool(false)View Answer
Explanation: instanceof can also be used to determine whether a variable is an instantiated object of a class that implements an interface.
7. What should be used to refer to a method in the context of a class rather than an object you use?
a) ->
b) __
c) $
d) ::
View Answer
Explanation: Example- parent::__construct()
8. Prior to which version of PHP did constructors took the name of the enclosing class.
a) PHP 4
b) PHP 5
c) PHP 5.3
d) PHP 5.4
View Answer
Explanation: The new unified constructors use the name __construct(). Using the old syntax, a call to a parent constructor would tie you to that particular class: parent::ShopProduct();
9. Which method or property can only be accessed from within the enclosing class? Even subclasses have no access.
a) public
b) friendly
c) private
d) protected
View Answer
Explanation: None.
10. A mutator method is also called as ___________
a) Setter
b) Accessor
c) Getter
d) Destructor
View Answer
Explanation: An accessor method is called getter. The common use of a mutator method is to initialise the value of member variables of a class.
Sanfoundry Global Education & Learning Series – PHP Programming.
To practice all areas of PHP Programming, here is complete set of 1000+ Multiple Choice Questions and Answers on PHP.
- Check PHP Books
- Practice Programming MCQs
- Practice MCA MCQs
- Check Information Technology Books
- Apply for Programming Internship