MCQs On Class 23 Exception Handling in PHP

Rashmi Mishra
0

 MCQs On Class 23

 Exception Handling in PHP

Here’s a comprehensive set of 100 multiple-choice questions (MCQs) related to exception handling in PHP, covering the fundamental concepts, syntax, and best practices. Each question is followed by four options, with the correct answer indicated.

MCQs on Exception Handling in PHP

1.   What keyword is used to define a block of code that may throw an exception in PHP?

o    A) try

o    B) catch

o    C) throw

o    D) finally
Answer: A) try

2.   Which keyword is used to handle an exception thrown by a try block?

o    A) try

o    B) catch

o    C) throw

o    D) finally
Answer: B) catch

3.   What is the purpose of the finally block in exception handling?

o    A) To catch exceptions

o    B) To execute code regardless of whether an exception is thrown

o    C) To define the exception type

o    D) To throw exceptions
Answer: B) To execute code regardless of whether an exception is thrown

4.   Which of the following is true about exceptions?

o    A) They are always fatal errors.

o    B) They can be caught and handled.

o    C) They cannot propagate up the call stack.

o    D) They are ignored by the PHP interpreter.
Answer: B) They can be caught and handled.

5.   Which of the following will throw an exception?

o    A) throw new Exception("Error!");

o    B) if (true) { throw new Exception(); }

o    C) Both A and B

o    D) None of the above
Answer: C) Both A and B

6.   What will the following code output?

php

Copy code

try {

    throw new Exception("Hello");

} catch (Exception $e) {

    echo $e->getMessage();

}

o    A) Exception

o    B) Hello

o    C) Error

o    D) NULL
Answer: B) Hello

7.   Which exception is thrown when attempting to divide by zero?

o    A) DivisionByZeroError

o    B) InvalidArgumentException

o    C) Exception

o    D) RuntimeException
Answer: A) DivisionByZeroError

8.   What will happen if you do not catch an exception?

o    A) The program will terminate.

o    B) The exception will be ignored.

o    C) A warning will be generated.

o    D) It will be logged.
Answer: A) The program will terminate.

9.   What will the following code print?

php

Copy code

try {

    echo 1/0;

} catch (Exception $e) {

    echo "Caught exception: ".$e->getMessage();

} finally {

    echo " Finally block.";

}

o    A) Caught exception: Division by zero

o    B) Caught exception:

o    C) 1 Finally block.

o    D) Finally block.
Answer: D) Finally block.

10.                     Which of the following is the correct way to create a custom exception class?

o    A) class MyException extends Error {}

o    B) class MyException extends Exception {}

o    C) class MyException implements Exception {}

o    D) class MyException extends Throwable {}
Answer: B) class MyException extends Exception {}

11.                     What keyword is used to throw an exception?

o    A) throw

o    B) catch

o    C) finally

o    D) handle
Answer: A) throw

12.                     What is the default behavior of an uncaught exception in PHP?

o    A) It will be logged.

o    B) The script will continue to run.

o    C) It will terminate the script.

o    D) It will trigger an error message.
Answer: C) It will terminate the script.

13.                     Which of the following methods can be used to get the file name of the exception?

o    A) getLine()

o    B) getFile()

o    C) getTrace()

o    D) getMessage()
Answer: B) getFile()

14.                     When should you use custom exceptions?

o    A) When you need to throw a built-in exception.

o    B) When you want to catch multiple exception types.

o    C) When you need to provide specific error handling for certain conditions.

o    D) Always; it's best practice.
Answer: C) When you need to provide specific error handling for certain conditions.

15.                     Which block is guaranteed to execute after try and catch?

o    A) try

o    B) catch

o    C) finally

o    D) throw
Answer: C) finally

16.                     What will the following code print?

php

Copy code

try {

    throw new Exception("Error!");

} catch (Exception $e) {

    echo "Caught: ".$e->getMessage();

} finally {

    echo " Done.";

}

o    A) Caught: Error! Done.

o    B) Caught: Error!

o    C) Error! Done.

o    D) Done.
Answer: A) Caught: Error! Done.

17.                     What type of exception is thrown by the file_get_contents() function if the file does not exist?

o    A) Exception

o    B) RuntimeException

o    C) Error

o    D) IOException
Answer: B) RuntimeException

18.                     What happens if an exception is thrown in the finally block?

o    A) It is ignored.

o    B) It replaces any previously thrown exception.

o    C) It does not affect the previous try-catch.

o    D) It will not be caught.
Answer: B) It replaces any previously thrown exception.

19.                     How do you create a custom exception class that also takes an error code?

o    A)

php

Copy code

class MyException extends Exception {

    public function __construct($message, $code) {

        parent::__construct($message, $code);

    }

}

o    B) class MyException extends Exception($message, $code) {}

o    C) class MyException implements Exception { function __construct() {} }

o    D) class MyException extends Error {}
Answer: A)

20.                     What does the getMessage() method return?

o    A) The line number where the exception occurred.

o    B) The message string for the exception.

o    C) The file name where the exception occurred.

o    D) The type of the exception.
Answer: B) The message string for the exception.

21.                     In which situation should you catch exceptions?

o    A) Always catch every exception.

o    B) Only catch exceptions that you can handle.

o    C) Only catch fatal errors.

o    D) Never catch exceptions.
Answer: B) Only catch exceptions that you can handle.

22.                     Which of the following will NOT trigger an exception?

o    A) throw new Exception();

o    B) die("Error");

o    C) throw new DivisionByZeroError();

o    D) trigger_error("Custom error", E_USER_ERROR);
Answer: B) die("Error");

23.                     What is the correct way to catch multiple exceptions?

o    A)

php

Copy code

catch (ExceptionType1 | ExceptionType2 $e) {}

o    B)

php

Copy code

catch (ExceptionType1 $e) {} catch (ExceptionType2 $e) {}

o    C)

php

Copy code

catch (ExceptionType1 and ExceptionType2 $e) {}

o    D)

php

Copy code

catch (Exception $e) {}

Answer: A)

24.                     Which method can be used to get the line number where the exception occurred?

o    A) getLine()

o    B) getTrace()

o    C) getMessage()

o    D) getFile()
Answer: A) getLine()

25.                     Which exception is thrown by the preg_match() function on invalid regex pattern?

o    A) RuntimeException

o    B) Exception

o    C) Error

o    D) ParseError
Answer: A) RuntimeException

26.                     What is the purpose of the throw statement in PHP?

o    A) To catch exceptions

o    B) To create new exceptions

o    C) To throw exceptions

o    D) To execute code
Answer: C) To throw exceptions

27.                     What is the consequence of having an uncaught exception?

o    A) The script continues to run.

o    B) A warning message is generated.

o    C) The script terminates and displays a fatal error.

o    D) The error is logged silently.
Answer: C) The script terminates and displays a fatal error.

28.                     Can you use the finally block without a try block?

o    A) Yes, it is allowed.

o    B) No, it must be preceded by a try block.

o    C) Only if the catch block is empty.

o    D) Yes, but only in PHP 8.0 and later.
Answer: B) No, it must be preceded by a try block.

29.                     Which of the following statements about exceptions is FALSE?

o    A) Exceptions can be chained.

o    B) You can throw any type of object as an exception.

o    C) All exceptions must be caught.

o    D) Exception handling improves code readability.
Answer: C) All exceptions must be caught.

30.                     What will happen if you catch an exception but do not handle it properly?

o    A) It will be ignored.

o    B) The program will stop executing.

o    C) It will cause a syntax error.

o    D) The exception will propagate up the call stack.
Answer: D) The exception will propagate up the call stack.

31.                     What is the purpose of custom exception classes?

o    A) To allow throwing exceptions.

o    B) To provide more specific error handling.

o    C) To increase performance.

o    D) To reduce code size.
Answer: B) To provide more specific error handling.

32.                     What type of error is a ParseError?

o    A) Runtime error

o    B) Compile-time error

o    C) Logical error

o    D) Syntax error
Answer: B) Compile-time error

33.                     How can you trigger an exception in PHP?

o    A) Using the throw statement

o    B) By returning false

o    C) Using the error_log() function

o    D) By calling exit()
Answer: A) Using the throw statement

34.                     Which method returns the stack trace of the exception?

o    A) getStackTrace()

o    B) getTraceAsString()

o    C) getTrace()

o    D) getStack()
Answer: B) getTraceAsString()

35.                     What will the following code output?

php

Copy code

try {

    throw new Exception("Error!");

} catch (Exception $e) {

    echo "Caught: ".$e->getMessage();

}

echo "End";

o    A) Caught: Error! End

o    B) Error! End

o    C) Caught: End

o    D) End
Answer: A) Caught: Error! End

36.                     What happens if an exception is thrown in the catch block?

o    A) It will be ignored.

o    B) It can be caught by another catch block.

o    C) The script will terminate.

o    D) It will not be thrown again.
Answer: B) It can be caught by another catch block.

37.                     Can you use multiple catch blocks for a single try block?

o    A) Yes, but only for the same exception type.

o    B) Yes, for different exception types.

o    C) No, only one catch block is allowed.

o    D) Yes, but they must be of different classes.
Answer: B) Yes, for different exception types.

38.                     What type of error is typically caught in a catch block?

o    A) Compile-time error

o    B) Runtime error

o    C) Logic error

o    D) All of the above
Answer: B) Runtime error

39.                     Which statement is true regarding the Exception class in PHP?

o    A) It cannot be extended.

o    B) It is an interface.

o    C) It is a base class for all exceptions.

o    D) It cannot be caught.
Answer: C) It is a base class for all exceptions.

40.                     What will happen if you attempt to throw a non-Exception object?

o    A) It will be automatically converted to an Exception.

o    B) A TypeError will be thrown.

o    C) It will be ignored.

o    D) The script will terminate.
Answer: B) A TypeError will be thrown.

41.                     What is the best practice when catching exceptions?

o    A) Always catch all exceptions.

o    B) Catch specific exceptions rather than generic ones.

o    C) Ignore all exceptions.

o    D) Log every exception without handling.
Answer: B) Catch specific exceptions rather than generic ones.

42.                     Which of the following is NOT a built-in exception class in PHP?

o    A) Exception

o    B) RuntimeException

o    C) LogicException

o    D) CustomException
Answer: D) CustomException

43.                     What will the following code output?

php

Copy code

class MyException extends Exception {}

try {

    throw new MyException("My custom error");

} catch (MyException $e) {

    echo $e->getMessage();

}

o    A) MyException

o    B) My custom error

o    C) Custom error

o    D) Error
Answer: B) My custom error

44.                     When should you avoid using exceptions?

o    A) For control flow in programs.

o    B) When you want to catch specific errors.

o    C) For expected error conditions.

o    D) For unexpected error conditions.
Answer: A) For control flow in programs.

45.                     What is a common use case for exception handling?

o    A) Handling input validation errors.

o    B) Managing business logic.

o    C) Catching syntax errors.

o    D) Redirecting users.
Answer: A) Handling input validation errors.

46.                     Which of the following can be used to log exceptions in PHP?

o    A) error_log()

o    B) log_exception()

o    C) log_errors()

o    D) write_log()
Answer: A) error_log()

47.                     What will the following code output?

php

Copy code

try {

    throw new Exception("Oops!");

} catch (Exception $e) {

    echo "Caught: " . $e->getMessage();

    throw new Exception("Another error!");

}

o    A) Caught: Oops! Another error!

o    B) Caught: Oops!

o    C) Another error!

o    D) Fatal error
Answer: D) Fatal error

48.                     Which function is used to create a user-defined error handler?

o    A) set_exception_handler()

o    B) set_error_handler()

o    C) register_shutdown_function()

o    D) handle_error()
Answer: A) set_exception_handler()

49.                     What is the correct syntax to throw an exception?

o    A) throw new Exception;

o    B) throw Exception();

o    C) throw Exception;

o    D) throw new Exception();
Answer: D) throw new Exception();

50.                     What happens if you do not include a finally block?

o    A) The script will not run.

o    B) The catch block will be ignored.

o    C) The try block may still execute.

o    D) The script terminates without executing any code.
Answer: C) The try block may still execute.

51.                     What will the following code print?

php

Copy code

function test() {

    throw new Exception("Test exception");

}

try {

    test();

} catch (Exception $e) {

    echo "Caught: ".$e->getMessage();

}

o    A) Caught: Test exception

o    B) Test exception

o    C) Caught:

o    D) Fatal error
Answer: A) Caught: Test exception

52.                     What is the difference between Error and Exception in PHP?

o    A) Errors cannot be caught, while exceptions can.

o    B) Both are the same.

o    C) Exceptions are always fatal, errors are not.

o    D) Errors are for syntax issues, exceptions are for runtime issues.
Answer: A) Errors cannot be caught, while exceptions can.

53.                     What will happen if you throw an exception and do not catch it?

o    A) It will be logged automatically.

o    B) The script will continue to run.

o    C) A fatal error will occur.

o    D) Nothing will happen.
Answer: C) A fatal error will occur.

54.                     Which of the following can be an argument to the throw statement?

o    A) An integer

o    B) A string

o    C) An object of type Exception

o    D) A boolean
Answer: C) An object of type Exception

55.                     Which PHP function allows you to register a function to handle uncaught exceptions?

o    A) set_error_handler()

o    B) set_exception_handler()

o    C) register_shutdown_function()

o    D) handle_uncaught_exceptions()
Answer: B) set_exception_handler()

56.                     Which of the following is a common exception type?

o    A) DivisionByZeroError

o    B) MissingMethodError

o    C) ClassNotFoundException

o    D) VariableNotDefinedException
Answer: A) DivisionByZeroError

57.                     What will the following code output?

php

Copy code

class CustomException extends Exception {}

try {

    throw new CustomException("Custom exception occurred");

} catch (CustomException $e) {

    echo $e->getMessage();

}

o    A) CustomException occurred

o    B) Custom exception occurred

o    C) Exception occurred

o    D) CustomException
Answer: B) Custom exception occurred

58.                     What does the finally block do?

o    A) It executes only if an exception is thrown.

o    B) It executes whether or not an exception is thrown.

o    C) It can only contain catch statements.

o    D) It cannot access variables from the try block.
Answer: B) It executes whether or not an exception is thrown.

59.                     What will the following code output?

php

Copy code

try {

    throw new Exception("Error!");

} catch (Exception $e) {

    echo "Caught: ".$e->getMessage();

    return;

}

echo "End";

o    A) Caught: Error! End

o    B) Caught: Error!

o    C) End

o    D) Fatal error
Answer: B) Caught: Error!

60.                     What is the purpose of the catch block?

o    A) To handle the exception thrown in the try block.

o    B) To create exceptions.

o    C) To terminate the program.

o    D) To log errors.
Answer: A) To handle the exception thrown in the try block.

61.                     Which of the following is NOT a valid way to create an exception?

o    A) throw new Exception("Error message");

o    B) throw new RuntimeException("Error message");

o    C) throw new LogicException("Error message");

o    D) throw Exception("Error message");
Answer: D) throw Exception("Error message");

62.                     What is the effect of using the try-catch block in PHP?

o    A) It prevents errors from occurring.

o    B) It allows for custom error handling.

o    C) It makes code run faster.

o    D) It creates new exceptions.
Answer: B) It allows for custom error handling.

63.                     What will happen if an exception is thrown within a finally block?

o    A) It will be ignored.

o    B) It will terminate the program.

o    C) It will be caught by an outer try-catch block.

o    D) It will replace the original exception.
Answer: D) It will replace the original exception.

64.                     What type of exception is a TypeError?

o    A) User-defined exception

o    B) Built-in exception

o    C) Fatal error

o    D) Parse error
Answer: B) Built-in exception

65.                     Which of the following statements is TRUE?

o    A) Exceptions can be used for control flow.

o    B) All exceptions must be caught.

o    C) Custom exceptions should always extend the Exception class.

o    D) The finally block is optional.
Answer: C) Custom exceptions should always extend the Exception class.

66.                     What will happen if an uncaught exception occurs in a script?

o    A) It will be logged.

o    B) The script will terminate and an error message will be displayed.

o    C) The script will continue executing.

o    D) A warning will be generated.
Answer: B) The script will terminate and an error message will be displayed.

67.                     Which statement correctly describes the Exception class?

o    A) It is an interface.

o    B) It is a built-in class for error handling.

o    C) It cannot be instantiated.

o    D) It cannot be extended.
Answer: B) It is a built-in class for error handling.

68.                     What happens when you catch an exception?

o    A) The exception is deleted.

o    B) The program continues without stopping.

o    C) The exception object is passed to the catch block.

o    D) The error is logged automatically.
Answer: C) The exception object is passed to the catch block.

69.                     Which keyword is used to define an exception class?

o    A) throw

o    B) catch

o    C) class

o    D) Exception
Answer: C) class

70.                     What will the following code output?

php

Copy code

function doSomething() {

    throw new Exception("Something went wrong");

}

try {

    doSomething();

} catch (Exception $e) {

    echo "Caught: " . $e->getMessage();

}

o    A) Caught: Something went wrong

o    B) Something went wrong

o    C) Caught:

o    D) Fatal error
Answer: A) Caught: Something went wrong

71.                     Which function can be used to create a custom error handler?

o    A) error_handler()

o    B) set_error_handler()

o    C) register_error_handler()

o    D) create_error_handler()
Answer: B) set_error_handler()

72.                     What is a fatal error in PHP?

o    A) A non-recoverable error that stops the script execution.

o    B) An error that can be handled with try-catch.

o    C) An error that is logged but does not stop the script.

o    D) A warning message displayed to the user.
Answer: A) A non-recoverable error that stops the script execution.

73.                     Which of the following cannot be thrown as an exception?

o    A) Exception

o    B) String

o    C) Integer

o    D) Custom exception class
Answer: B) String

74.                     How can you retrieve the line number where an exception was thrown?

o    A) getLineNumber()

o    B) getTraceAsString()

o    C) getLine()

o    D) getFile()
Answer: C) getLine()

75.                     What is the difference between a logic error and a runtime error?

o    A) Logic errors are ignored, runtime errors terminate the program.

o    B) Logic errors occur at compile time, runtime errors occur during execution.

o    C) Logic errors are the same as syntax errors.

o    D) Logic errors are identified by the compiler, runtime errors are not.
Answer: D) Logic errors are identified by the compiler, runtime errors are not.

76.                     What is the main advantage of using exceptions?

o    A) They are faster than regular error handling.

o    B) They provide a clean and manageable way to handle errors.

o    C) They allow you to avoid writing error handling code.

o    D) They are less complex than traditional error handling.
Answer: B) They provide a clean and manageable way to handle errors.

77.                     Which statement about exceptions is TRUE?

o    A) Exceptions are ignored if not caught.

o    B) You can catch multiple exceptions in a single catch block.

o    C) Exceptions can only be caught at the end of a script.

o    D) Exceptions are objects of classes.
Answer: D) Exceptions are objects of classes.

78.                     How does PHP treat uncaught exceptions?

o    A) They are logged silently.

o    B) They stop the script execution and display an error message.

o    C) They are ignored.

o    D) They are converted to warnings.
Answer: B) They stop the script execution and display an error message.

79.                     What will the following code output?

php

Copy code

try {

    throw new Exception("Test");

} catch (Exception $e) {

    echo "Caught: " . $e->getMessage();

}

o    A) Caught:

o    B) Caught: Test

o    C) Test

o    D) No output
Answer: B) Caught: Test

80.                     What is the purpose of the getMessage() method in the Exception class?

o    A) To get the type of exception.

o    B) To get a string representation of the exception.

o    C) To get the error code.

o    D) To retrieve a custom error message.
Answer: D) To retrieve a custom error message.

81.                     Which statement about the finally block is correct?

o    A) It executes only if the try block succeeds.

o    B) It cannot contain return statements.

o    C) It always executes after the try and catch blocks.

o    D) It can only be used with try blocks.
Answer: C) It always executes after the try and catch blocks.

82.                     What will the following code output?

php

Copy code

try {

    throw new Exception("An error occurred");

} catch (Exception $e) {

    echo "Error: " . $e->getMessage();

} finally {

    echo "Finally block executed";

}

o    A) Error: An error occurred

o    B) Finally block executed

o    C) Error: An error occurredFinally block executed

o    D) Fatal error
Answer: C) Error: An error occurredFinally block executed

83.                     What happens if an exception is thrown inside a catch block?

o    A) It will be caught by the same catch block.

o    B) It will terminate the script.

o    C) It can be caught by an outer try-catch block.

o    D) It will be ignored.
Answer: C) It can be caught by an outer try-catch block.

84.                     How can you define a custom exception class?

o    A) class MyException extends Exception {}

o    B) class MyException implements Exception {}

o    C) class MyException is Exception {}

o    D) class MyException inherits Exception {}
Answer: A) class MyException extends Exception {}

85.                     What is the output of the following code?

php

Copy code

try {

    throw new Exception("Test Exception");

} catch (Exception $e) {

    echo $e->getMessage();

    throw new Exception("Another Exception");

}

o    A) Test Exception

o    B) Another Exception

o    C) Fatal error

o    D) Test ExceptionAnother Exception
Answer: D) Test ExceptionAnother Exception

86.                     What does getTraceAsString() do in the Exception class?

o    A) Returns the line number where the exception occurred.

o    B) Returns a string representation of the stack trace.

o    C) Returns the error code of the exception.

o    D) Returns the file name where the exception occurred.
Answer: B) Returns a string representation of the stack trace.

87.                     What will happen if you do not catch an exception?

o    A) The program will continue executing normally.

o    B) The exception will be ignored.

o    C) A warning will be generated.

o    D) The program will terminate, and an error message will be displayed.
Answer: D) The program will terminate, and an error message will be displayed.

88.                     Which method is used to retrieve the file name where the exception was created?

o    A) getFileName()

o    B) getFile()

o    C) getTrace()

o    D) getLine()
Answer: B) getFile()

89.                     What is the difference between throw and catch?

o    A) throw is used to handle exceptions; catch is used to create exceptions.

o    B) throw is used to create exceptions; catch is used to handle them.

o    C) Both are used interchangeably.

o    D) throw is used in the finally block; catch is used in the try block.
Answer: B) throw is used to create exceptions; catch is used to handle them.

90.                     What is the output of the following code?

php

Copy code

function divide($a, $b) {

    if ($b == 0) {

        throw new DivisionByZeroError("Division by zero");

    }

    return $a / $b;

}

try {

    divide(10, 0);

} catch (DivisionByZeroError $e) {

    echo $e->getMessage();

}

o    A) Division by zero

o    B) 10

o    C) Fatal error

o    D) No output
Answer: A) Division by zero


Tags

Post a Comment

0Comments

Post a Comment (0)