PHP Exception Handling MCQ

1. What is Exception Handling in PHP?

a) Handling HTML exceptions
b) A method to handle errors and exceptions in a program
c) A syntax for declaring variables
d) Handling database exceptions only

Answer:

b) A method to handle errors and exceptions in a program

Explanation:

Exception handling in PHP is a way to handle errors and exceptions gracefully during the execution of a program.

2. Which keyword is used to throw an exception in PHP?

a) throw
b) catch
c) try
d) error

Answer:

a) throw

Explanation:

The 'throw' keyword is used to throw an exception in PHP.

3. What is the purpose of the try block in PHP?

a) To execute code that may cause an exception
b) To catch exceptions
c) To finally execute code regardless of exceptions
d) To declare exceptions

Answer:

a) To execute code that may cause an exception

Explanation:

A try block is used to wrap the code that may potentially cause an exception.

4. What does the catch block do in PHP?

a) Catches errors that occur in the script
b) Catches exceptions thrown within the try block
c) Catches user input errors
d) Catches database errors

Answer:

b) Catches exceptions thrown within the try block

Explanation:

The catch block is used to handle exceptions that are thrown within the associated try block.

5. How do you create a custom exception in PHP?

a) By using the exception() function
b) By extending the Exception class
c) By declaring a new exception variable
d) By using the custom_exception() function

Answer:

b) By extending the Exception class

Explanation:

Custom exceptions are created in PHP by extending the built-in Exception class.

6. Can multiple catch blocks be used with a single try block?

a) Yes
b) No
c) Only in PHP 7 and later
d) Only with custom exceptions

Answer:

a) Yes

Explanation:

Multiple catch blocks can be used with a single try block to handle different types of exceptions.

7. What is the purpose of the finally block in PHP?

a) To execute code after try and catch blocks, regardless of whether an exception was thrown
b) To finalize exception handling
c) To clean up resources
d) To throw the final exception

Answer:

a) To execute code after try and catch blocks, regardless of whether an exception was thrown

Explanation:

The finally block is used to execute code after try and catch blocks, regardless of whether an exception was thrown or not.

8. Which class do all exceptions extend in PHP?

a) Error
b) MyException
c) Exception
d) Throwable

Answer:

c) Exception

Explanation:

All exceptions in PHP extend the base Exception class.

9. What happens if an exception is not caught in PHP?

a) The script continues execution
b) A default exception handler is called
c) The script terminates with a fatal error
d) The exception is ignored

Answer:

c) The script terminates with a fatal error

Explanation:

If an exception is not caught, the script will terminate with a fatal error.

10. What is the correct way to specify the type of exception to catch in PHP?

a) catch(ExceptionType $e) { … }
b) catch as ExceptionType $e { … }
c) catch in ExceptionType $e { … }
d) catch with ExceptionType $e { … }

Answer:

a) catch(ExceptionType $e) { … }

Explanation:

The catch block specifies the type of exception to catch using catch(ExceptionType $e) syntax.

11. How can you rethrow an exception in PHP?

a) By using the rethrow keyword
b) By calling throw again within a catch block
c) By using the continue keyword
d) Rethrowing exceptions is not possible in PHP

Answer:

b) By calling throw again within a catch block

Explanation:

An exception can be rethrown in PHP by using the throw keyword again within a catch block.

12. What is the primary purpose of exception handling in PHP?

a) To optimize code performance
b) To improve code readability
c) To handle runtime errors gracefully
d) To debug code

Answer:

c) To handle runtime errors gracefully

Explanation:

The primary purpose of exception handling is to handle runtime errors in a way that does not crash the program and allows for graceful recovery or termination.

13. Which of the following is a built-in exception in PHP?

a) UserException
b) DatabaseException
c) InvalidArgumentException
d) FormException

Answer:

c) InvalidArgumentException

Explanation:

InvalidArgumentException is an example of a built-in exception in PHP.

14. Can you nest try-catch blocks in PHP?

a) Yes
b) No
c) Only in classes
d) Only in functions

Answer:

a) Yes

Explanation:

Try-catch blocks can be nested within each other in PHP.

15. What is the output of getMessage() function in an exception object in PHP?

a) The line number where the exception occurred
b) The exception message
c) The stack trace
d) The exception code

Answer:

b) The exception message

Explanation:

The getMessage() function returns the message of the exception that was thrown.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top