PHP Multiple Choice Questions – Object Tools

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

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

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

a) i)
b) ii)
c) iii)
d) i), ii) & iii)
View Answer

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

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

Answer: b
Explanation: Example – namespace com\getinstance\util;

6. Output:

advertisement
    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

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

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

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

  1. namespace main;
  2. use com\getinstance\util\Debug;
  3.  
  4. class Debug {
  5.     static function helloWorld() {
  6.         print "hello from main\Debug";
  7.     } 
  8. }
  9.  
  10. Debug::helloWorld();

a) error
b) hello from main
c) hello from main\Debug
d) debug
View Answer

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

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

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

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.