PHP Questions and Answers Part-15

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

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

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

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

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();

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

Answer: a
Explanation: The function getMessage() gets the exception message. It returns the message if it is passed to the constructor.

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

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

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.

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

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
a) Only ii)
b) Only iii)
c) i), ii), iii) and iv)
d) Only iv)

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)

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.