PHP Exception Handling MCQs
PHP Exception Handling MCQs : This section focuses on "Exception Handling" in PHP. These Multiple Choice Questions (mcq) and answers should be practiced to improve the PHP skills required for various interviews (campus interview, walk-in interview, company interview), placements, entrance exam and other competitive examinations.
1. In Which php version Exception handling was added?
A. PHP 5.1
B. PHP 5.2
C. PHP 5.3
D. PHP 5
View Answer
Ans : D
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. five
B. six
C. seven
D. eight
View Answer
Ans : C
Explanation: The seven methods are: getCode(), getFile(), getLine(), getMessage(), getPrevious(), getTrace(), getTraceAsString().
3. what is try block do?
A. It represent block of code in which exception can arise.
B. It is used in place of catch block or after catch block .
C. It is used to throw an exception.
D. None of the above
View Answer
Ans : A
Explanation: try: It represent block of code in which exception can arise.
4. what is finally block do?
A. It represent block of code in which exception can arise.
B. It is used in place of catch block or after catch block .
C. It is used to throw an exception.
D. None of the above
View Answer
Ans : B
Explanation: finally: It is used in place of catch block or after catch block basically it is put for cleanup activity in PHP code.
5. what is throw block do?
A. It represent block of code in which exception can arise.
B. It is used in place of catch block or after catch block .
C. It is used to throw an exception.
D. None of the above
View Answer
Ans : C
Explanation: throw: It is used to throw an exception. It is also used to list the exceptions that a function throws, but doesn't handle itself.
6. 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
Ans : B
Explanation: throw new Exception(); trigger an exception and each throw must have at least one catch.
7. Why Exception Handling in PHP used?
A. Separation of error handling code from normal code
B. Grouping of error types
C. Both A And B
D. None of the above
View Answer
Ans : C
Explanation: Exception Handling in PHP used for Separation of error handling code from normal code and Grouping of error types.
8. Which of the following is not specialized keywords in exception handling.
A. try
B. catch
C. throw
D. this
View Answer
Ans : D
Explanation: this keyword is not specialized keywords in exception handling.
9. How many error levels are available in PHP?
A. 14
B. 15
C. 16
D. 17
View Answer
Ans : C
Explanation: There are 16 error levels available in PHP.
10. What is the description of Error level E_ERROR?
A. Fatal run-time error
B. Near-fatal error
C. Compile-time error
D. Fatal Compile-time error
View Answer
Ans : A
Explanation: A Fatal run-time error is the description of Error level E_ERROR.
11. Nanda, a PHP programmer is checking for uninitialized variables and would want to throw an exception if any with an error code 5. Identify the correct statement Nanda should write
A. throw new Exception(5,"Uninitialized Variable")
B. new Exception(5,"Uninitialized Variable")
C. throw new Exception("Uninitialized Variable",5)
D. throw new Exception("Uninitialized Variable")
View Answer
Ans : C
Explanation: The correct statement Nanda should write is throw new Exception("Uninitialized Variable",5).
12. Rimo is handling the exception raised by Nanda and wants to print the appropriate error message and code. Identify the correct catch block
A. catch() { echo getMessage(); echo getCode(); }
B. catch(Exception $e) { echo $e->getMessage(); echo $e->getCode(); }
C. catch(Exception $e) { echo "Message:Uninitialized Variable"; echo "Code:5"; }
D. None of the above
View Answer
Ans : B
Explanation: The correct catch block catch(Exception $e) { echo $e->getMessage(); echo $e->getCode(); }
13. Which of the following are valid statements to raise an exception
A. throw new exception()
B. throw new exception("Error Occured")
C. throw new exception("Error Occured",5)
D. All of the above
View Answer
Ans : D
Explanation: All three options A, B, C are valid statements to raise an exception.
14. Which of the below statements are false?
A. Exception class can be inherited
B. The methods in Exception class are defined as abstract
C. The methods in Exception class are defined as final
D. Sub classess of Exception, can define new methods to extend the functionality
View Answer
Ans : C
Explanation: The methods in Exception class are defined as final statements are false.
15. Which is the error level to report probable run-time errors?
A. E_ERROR
B. E_NOTICE
C. E_CORE
D. E_USER_ERROR
View Answer
Ans : B
Explanation: E_NOTICE is the error level to report probable run-time errors
16. What is the advantage of log_errors directive?
A. Helps in trouble shooting of the application
B. Displays error messages on the browser
C. Displays only errors which meet the criteria
D. None of the above
View Answer
Ans : A
Explanation: Helps in trouble shooting of the application is the advantage of log_errors directive
17. How can errors be logged in files?
A. Turn on log_errors
B. Set error_log to file name
C. Both A and B
D. None of the above
View Answer
Ans : C
Explanation: Turn on log_errors, Set error_log to file name can use to logged errors in files.
18. Which is the error level to report errors during PHP startup engine?
A. E_CORE_ERROR
B. E_ERROR
C. E_COMPILE_ERROR
D. E_CORE
View Answer
Ans : A
Explanation: E_CORE_ERROR is the error level to report errors during PHP startup engine.
19. Which is the correct method to turn off error display?
A. ini_set("Off")
B. init_set("error_reporting","Off")
C. error_reporting=Off
D. init_set("error_reporting=Off")
View Answer
Ans : B
Explanation: init_set("error_reporting","Off") is the correct method to turn off error display.
20. 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
Ans : 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.
Also check :
Discussion