PHP Questions & Answers – Exception Handling

This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “Exception Handling”.

1. Which version of PHP was added with Exception handling?
a) PHP 4
b) PHP 5
c) PHP 5.3
d) PHP 6
View Answer

Answer: b
Explanation: Exception handling was added to PHP with the version 5 release, and further enhanced with version 5.3.

2. How many methods are available for the exception class?
a) 5
b) 6
c) 7
d) 8
View Answer

Answer: c
Explanation: The seven methods are: getCode(), getFile(), getLine(), getMessage(), getPrevious(), getTrace(), getTraceAsString().

3. Which version added the method getPrevious()?
a) PHP 4
b) PHP 5
c) PHP 5.1
d) PHP 5.3
View Answer

Answer: d
Explanation: The function getPrevious() returns previous exception.
advertisement
advertisement

4. Which of the following statements invoke the exception class?
a) throws new Exception();
b) throw new Exception();
c) new Exception();
d) new throws Exception();
View Answer

Answer: b
Explanation: throw new Exception(); trigger an exception and each “throw” must have at least one “catch”.

5. Which one of the following is the right description for the method getMessage()?
a) Returns the message if it is passed to the constructor
b) Returns the message if it is passed to the class
c) Returns the message if it is passed to the file
d) Returns the message if it is passed to the object
View Answer

Answer: a
Explanation: The function getMessage() gets the exception message. It returns the message if it is passed to the constructor.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

6. You can extend the exception base class, but you cannot override any of the preceding methods because the are declared as__________
a) protected
b) final
c) static
d) private
View Answer

Answer: b
Explanation: Marking a method as final prevents it from being overridden by a subclass.

7. What does SPL stand for?
a) Standard PHP Library
b) Source PHP Library
c) Standard PHP List
d) Source PHP List
View Answer

Answer: a
Explanation: The standard PHP library(SPL) extends PHP by offering ready-made solutions to commonplace tasks such as file access, iteration of various sorts etc.
advertisement

8. How many predefined exceptions does SPL provide access to?
a) 13
b) 14
c) 15
d) 16
View Answer

Answer: a
Explanation: It provides 13 exceptions: BadFunctionCallException, BadMethodCallException, DomainException, InvalidArgumentException, LengthException, LogicException, OutOfBoundsException, OutOfRangeException, OverflowException, RangeException, RuntimeException, UnderflowException, UnexpectedValueException.

9. Which of the following is/are an exception?

i) BadFunctionCallException
ii) BadMethodCallException
iii) LogicException
iv) DomainException
advertisement

a) Only ii)
b) Only iii)
c) i), ii), iii) and iv)
d) Only iv)
View Answer

Answer: c
Explanation: There are total 13 types of predefined exceptions SPL provide access to. BadFunctionCallException,BadMethodCallExceptio, LogicException, DomainException are also included in the exceptions.

10. Which of the following is/are an exception?

i) OutOfBoundException
ii) OutOfRangeException
iii) OverflowException
iv) UnderflowException

a) i)
b) i) and iii)
c) i) and ii)
d) i), ii), iii) and iv)
View Answer

Answer: d
Explanation: The exception is thrown if a value is not a valid key. This represents errors that cannot be detected at compile time. OutOfBoundException, OutOfRangeException, OverflowException, UnderflowException are valid exceptions in PHP.

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.