PHP Multiple Choice Questions & Answers 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
Answer: c
Clarification: 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
Answer: a
Clarification: 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?
-
-
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)
Answer: c
Clarification: 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?
-
-
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)
Answer: b
Clarification: 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?
-
-
class MyClass
-
{
-
}
-
-
$a = new MyClass;
-
var_dump(!($a instanceof stdClass));
-
?>
a) bool(true)
b) bool(false)
c) error
d) none of the mentioned
Answer: a
Clarification: 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?
-
-
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)
Answer: b
Clarification: 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) ::
Answer: d
Clarification: 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
Answer: b
Clarification: 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
Answer: c
Clarification: None.
10. A mutator method is also called as ___________
a) Setter
b) Accessor
c) Getter
d) Destructor
Answer: a
Clarification: An accessor method is called getter. The common use of a mutator method is to initialise the value of member variables of a class.
To practice all areas of PHP,