This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “Object Tools”.
1. A package is a set of related _________
a) Objects
b) Classes
c) Programs
d) Functions
View Answer
Explanation: A package is a set of related classes, usually grouped together in some way. Packages can be used to separate parts of a system from one another.
2. Till which version of PHP, developers were forced to name their files in a global context?
a) PHP 4
b) PHP 5
c) PHP 5.2
d) PHP 5.3
View Answer
Explanation: If you named a class ShoppingBasket, it would become instantly available across your system.
3. Which of the following can you place inside a namespace?
i) classes ii) functions iii) variables
a) i)
b) ii)
c) iii)
d) i), ii) & iii)
View Answer
Explanation: A namespace is a bucket in which you can place your classes, functions and variables.
4. Which one of the following is the correct way of declaring a namespace?
a) namespace my;
b) namespace my();
c) my namespace;
d) namespace(my);
View Answer
Explanation: The namespace declaration must be the first statement in its file.
5. Which symbol is used to declare nested namespaces?
a) /
b) \
c) .
d) |
View Answer
Explanation: Example – namespace com\getinstance\util;
6. Output:
namespace main; com\getinstance\util\Debug::helloWorld() PHP Fatal error: Class 'main\com\getinstance\util\Debug' not found in ...
Using which one of the following PHP lines will the error be removed?
a) \\com\getinstance\util\Debug::helloWorld();
b) getinstance\util\Debug::helloWorld();
c) main.com\getinstance\util\Debug::helloWorld();
d) \com\getinstance\util\Debug::helloWorld();
View Answer
Explanation: PHP is looking below the namespace main for com\getinstance\util and not finding it. That’s because we are using a relative namespace here. Just as you can make absolute URLs and filepaths by starting off with a separator so you can with namespaces.
7. Which keyword can be used to fix the following PHP error?
namespace main; com\getinstance\util\Debug::helloWorld() PHP Fatal error: Class 'main\com\getinstance\util\Debug' not found in ...
a) fix
b) join
c) use
d) namespace
View Answer
Explanation: Use keyword allows you to alias other namespaces within the current namespace.
Example – namespace main;
use com\getinstance\util; util\Debug::helloWorld();
8. If I already had a Debug class in the main namespace. What will be the output of the following PHP code?
namespace main;
use com\getinstance\util\Debug;
class Debug {
static function helloWorld() {
print "hello from main\Debug";
}
}
Debug::helloWorld();
a) error
b) hello from main
c) hello from main\Debug
d) debug
View Answer
Explanation: PHP Fatal error: Cannot declare class main\Debug because the name is already in use.
9. Which one of the following statements is true for include_once() and require_once()?
a) Both are exactly the same
b) include_once is used for files where as require_once() is not
c) Both Handle the errors in the same way
d) Both do not handle the errors in the same way
View Answer
Explanation: The only difference between the include() and require() statements lies in their handling of errors. A file invoked using require() will bring down your entire process when you meet an error. The same error encountered via a call to include() will merely generate a warning and end execution of the included file.
10. Which one of the following statements is true for require() and require_once()?
a) They are functions
b) They are statements
c) They’ll not work if the () is not present
d) They can not be used to require files
View Answer
Explanation: require() and require_once() are actually statements, not functions. This means that you can omit the brackets when using them.
More MCQs on PHP Object Tools:
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.
- Check Information Technology Books
- Check PHP Books
- Practice MCA MCQs
- Apply for Programming Internship
- Practice Programming MCQs