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?
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?
d) as many times as needed
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?
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.
4. Which one of the following will you use to check the class of an object?
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?
Explanation: As of PHP 5.3 is_a() no longer deprecated.
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?
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?
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 argument to the function method_exist()?
a) Method does not exist
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?
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.
10. Which class accepts a class name or an object reference and returns an array of interface name?
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.