PHP Multiple Choice Questions on “Object Tools – 1”.
1. A package is a set of related _________
a) Objects
b) Classes
c) Programs
d) Functions
Answer: b
Clarification: 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
Answer: d
Clarification: 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)
Answer: d
Clarification: 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);
Answer: a
Clarification: The namespace declaration must be the first statement in its file.
5. Which symbol is used to declare nested namespaces?
a) /
b)
c) .
d) |
Answer: b
Clarification: Example – namespace comgetinstanceutil;
6. Output:
namespace main; comgetinstanceutilDebug::helloWorld() PHP Fatal error: Class 'maincomgetinstanceutilDebug' not found in ...
Using which one of the following PHP lines will the error be removed?
a) \comgetinstanceutilDebug::helloWorld();
b) getinstanceutilDebug::helloWorld();
c) main.comgetinstanceutilDebug::helloWorld();
d) comgetinstanceutilDebug::helloWorld();
Answer: d
Clarification: PHP is looking below the namespace main for comgetinstanceutil 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; comgetinstanceutilDebug::helloWorld() PHP Fatal error: Class 'maincomgetinstanceutilDebug' not found in ...
a) fix
b) join
c) use
d) namespace
Answer: c
Clarification: Use keyword allows you to alias other namespaces within the current namespace.
Example – namespace main;
use comgetinstanceutil; utilDebug::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 comgetinstanceutilDebug;
-
class Debug {
-
static function helloWorld() {
-
print "hello from mainDebug";
-
} -
} -
Debug::helloWorld();
a) error
b) hello from main
c) hello from mainDebug
d) debug
Answer: a
Clarification: PHP Fatal error: Cannot declare class mainDebug 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
Answer: d
Clarification: 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
Answer: b
Clarification: require() and require_once() are actually statements, not functions. This means that you can omit the brackets when using them.
