PHP Questions & Answers – Object Tools – 2

This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “Object Tools – 2”.

1. Which function was introduced to help automate the inclusion of class files?
a) __load()
b) __preload()
c) __autoload()
d) __inload()
View Answer

Answer: c
Explanation: When the PHP engine encounters an attempt to instantiate an unknown class, it invokes the __autoload() function, passing it the class name as a string. It is up to the implementer to define a strategy for locating and including the missing class file.

2. How many times can you define _________ autoload in a process?
a) once
b) twice
c) thrice
d) as many times as needed
View Answer

Answer: a
Explanation: __autoload is a powerful tool, but it does have some limitations. In particular, you can only define it once in a process. If you need to change your autoload function dynamically you should look at the spl_autoload_register function, which supports that functionality.

3. Which one of the following functions will you use to check that the class exists before you work with it?
a) class_exist()
b) class_exists()
c) exist()
d) exists_class()
View Answer

Answer: b
Explanation: The class_exists() function accepts a string representing the class to check for and returns a Boolean true value if the class exists and false otherwise.
advertisement

4. Which one of the following will you use to check the class of an object?
a) class()
b) _class()
c) class_check()
d) get_class()
View Answer

Answer: d
Explanation: This function accepts any object as an argument and returns its class name as a string.

5. PHP 4 did not support instanceof. Instead, which function did it provide?
a) is()
b) get_class()
c) is_a()
d) is_the()
View Answer

Answer: c
Explanation: As of PHP 5.3 is_a() no longer deprecated.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

6. You use the get_class_methods() function to return the names of all the methods in the class. Which function will you use to print it on the screen?
a) printf()
b) print_ar
c) print_r
d) echo
View Answer

Answer: c
Explanation: The function get_class_methods returns an array therefore we cannot use echo.

7. If you call a method and it doesn’t exist it’ll cause a problem. To check the method which function will you use?
a) _method()
b) methodexists()
c) is_callable()
d) is_method()
View Answer

Answer: c
Explanation: There is another method which you can use – method_exists() for similar purpose.

8. What will be the output if a protected method is given as the argument to the function method_exist()?
a) Method does not exist
b) False
c) Error
d) True
View Answer

Answer: d
Explanation: method_exists() returns true for private and protected methods as well as for public ones. Remember that the fact that a method exists does not mean that it will be callable.

9. Which one of the following function should I use to find the parent class of a class?
a) get_parent_class()
b) parent_class()
c) class_parent()
d) get_class_parent()
View Answer

Answer: a
Explanation: The class functions also allow us to chart inheritance relationships.This function requires either an object or a class name, and it returns the name of the superclass, if any. If no such class exists, that is, if the class we are testing does not have a parent, then the function returns false.
advertisement

10. Which class accepts a class name or an object reference and returns an array of interface name?
a) class_implements()
b) is_subclass()
c) is_subclass_of()
d) class_interface()
View Answer

Answer: a
Explanation: is_subclass_of() will tell you only about class inheritance relationships. It will not tell you that a class implements an interface.

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.

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.