PHP Questions & Answers – Object Basics-2

This set of PHP Multiple Choice Questions & Answers 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

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

Answer: a
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?

advertisement
advertisement
  1. <?php
  2. class MyClass
  3. {
  4. }
  5.  
  6. class NotMyClass
  7. {
  8. }
  9. $a = new MyClass;
  10.  
  11. var_dump($a instanceof MyClass);
  12. var_dump($a instanceof NotMyClass);
  13. ?>

a)

bool(true)
bool(true)
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

b)

bool(false)
bool(false)

c)

bool(true)
bool(false)
advertisement

d)

bool(false)
bool(true)
View Answer
Answer: c
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?

advertisement
  1. <?php
  2. class ParentClass
  3. {
  4. }
  5.  
  6. class MyClass extends ParentClass
  7. {
  8. }
  9.  
  10. $a = new MyClass;
  11.  
  12. var_dump($a instanceof MyClass);
  13. var_dump($a instanceof ParentClass);
  14. ?>

a)

bool(false)
bool(false)

b)

bool(true)
bool(true)

c)

bool(false)
bool(true)

d)

bool(true)
bool(false)
View Answer
Answer: b
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?

  1. <?php
  2. class MyClass
  3. {
  4. }
  5.  
  6. $a = new MyClass;
  7. var_dump(!($a instanceof stdClass));
  8. ?>

a) bool(true)
b) bool(false)
c) error
d) none of the mentioned
View Answer

Answer: a
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?

  1. <?php
  2. interface MyInterface
  3. {
  4. }
  5.  
  6. class MyClass implements MyInterface
  7. {
  8. }
  9.  
  10. $a = new MyClass;
  11.  
  12. var_dump($a instanceof MyClass);
  13. var_dump($a instanceof MyInterface);
  14. ?>

a)

bool(false)
bool(false)

b)

bool(true)
bool(true)

c)

bool(false)
bool(true)

d)

bool(true)
bool(false)
View Answer
Answer: b
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

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

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

Answer: c
Explanation: None.

10. A mutator method is also called as ___________
a) Setter
b) Accessor
c) Getter
d) Destructor
View Answer

Answer: a
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, 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]

advertisement
advertisement
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.