MCQs on Exception Handling | Python programming

1. Introduction to Exceptions

  1. What is an exception in Python?
    • a) A bug that occurs in the code
    • b) An event that disrupts the normal flow of the program
    • c) A special type of variable
    • d) A syntax error
  2. What happens when an exception is raised in Python?
    • a) The program stops running immediately
    • b) The program runs indefinitely
    • c) The program skips to the next statement
    • d) The program catches the exception and continues
  3. Which of the following is an example of an exception in Python?
    • a) IndexError
    • b) undefined_variable
    • c) out_of_memory
    • d) InfiniteLoop
  4. Which of the following Python errors indicates that you are trying to divide a number by zero?
    • a) ValueError
    • b) TypeError
    • c) ZeroDivisionError
    • d) IndexError
  5. How does Python handle exceptions?
    • a) By terminating the program
    • b) By printing the exception message and continuing
    • c) By ignoring the exception
    • d) By suspending the program

2. Try, Except, Else, Finally Blocks

  1. What is the purpose of the try block in Python?
    • a) To test the code for exceptions
    • b) To execute code that is always successful
    • c) To handle the exception if it occurs
    • d) To specify the return value of a function
  2. Which block is executed when no exception occurs in the try block?
    • a) except
    • b) else
    • c) finally
    • d) raise
  3. What will happen if an exception is raised in the try block and there is no except block to catch it?
    • a) Python will stop the program
    • b) The program will continue to run after printing the error message
    • c) The exception is ignored
    • d) Python will ask the user for input
  4. What is the role of the finally block in exception handling?
    • a) It runs if no exception occurs
    • b) It runs if an exception occurs
    • c) It always runs, regardless of whether an exception occurs or not
    • d) It only runs if the exception is caught
  5. Which of the following is the correct syntax for using try, except, else, and finally?
    • a) try: code except: code else: code finally: code
    • b) try: code except: code else: code
    • c) try: code except: code finally: code
    • d) try: code except: code

3. Custom Exceptions

  1. How do you define a custom exception in Python?
    • a) By creating a function
    • b) By subclassing the built-in Exception class
    • c) By using the raise keyword
    • d) By calling Exception("Custom message")
  2. Which of the following is the correct way to define a custom exception?
    • a) class MyException: pass
    • b) class MyException(Exception): pass
    • c) class MyException(BaseException): pass
    • d) class MyException(Exception) { pass }
  3. How can you raise a custom exception in Python?
    • a) raise MyException()
    • b) raise Exception()
    • c) throw MyException()
    • d) trigger MyException()
  4. What is the correct way to handle a custom exception in a try-except block?
    • a) except MyException:
    • b) except MyException()
    • c) catch MyException
    • d) except MyException as e
  5. In the following code, what is the purpose of raise?
    if condition: raise ValueError("A custom error occurred")
    • a) It terminates the program
    • b) It throws an exception
    • c) It skips the condition
    • d) It prints an error message

4. Raising Exceptions

  1. Which of the following is the correct syntax for raising an exception?
    • a) throw Exception()
    • b) raise Exception()
    • c) raise Error()
    • d) signal Exception()
  2. What happens if you raise an exception inside a try block?
    • a) The program continues to the next statement
    • b) The program terminates immediately
    • c) The exception is passed to the except block for handling
    • d) The finally block is skipped
  3. How can you raise a custom exception with a specific message in Python?
    • a) raise Exception("Custom message")
    • b) throw Exception("Custom message")
    • c) raise ValueError("Custom message")
    • d) signal Exception("Custom message")
  4. Which of the following statements raises an exception intentionally when a condition is met?
    • a) if x < 10: raise Exception("x is too small")
    • b) if x < 10: raise ValueError("x is too small")
    • c) if x < 10: raise MyException("x is too small")
    • d) All of the above
  5. Can a function raise an exception to be handled by a calling function?
    • a) No, only the calling function can raise an exception
    • b) Yes, a function can raise an exception
    • c) Only built-in exceptions can be raised
    • d) No, exceptions can only be raised in the global scope

5. Assertions

  1. What is the purpose of assertions in Python?
    • a) To check if a condition is true and raise an exception if it is false
    • b) To handle all exceptions automatically
    • c) To define custom exceptions
    • d) To raise an exception in a try block
  2. Which function is used for assertion in Python?
    • a) assert()
    • b) assertEqual()
    • c) check()
    • d) assertTrue()
  3. What happens if an assertion fails in Python?
    • a) The program continues running normally
    • b) An AssertionError is raised
    • c) The program terminates immediately without any message
    • d) A custom exception is raised
  4. Which of the following is an example of a correct assertion statement in Python?
    • a) assert x == 10
    • b) assert(x == 10)
    • c) assert 10 == x
    • d) assert (x == 10)
  5. What is the correct behavior when running Python in optimized mode (using python -O) regarding assertions?
    • a) Assertions are executed normally
    • b) Assertions are ignored
    • c) Assertions are automatically handled by Python
    • d) Assertions are logged into a file
  6. What is the output if the following code executes successfully?
    x = 5 assert x > 3, "x is less than or equal to 3"
    • a) “x is less than or equal to 3”
    • b) No output, execution continues
    • c) AssertionError is raised
    • d) “x is greater than 3”
  7. Can assertions be used to catch exceptions raised by code?
    • a) Yes, they are useful for handling errors
    • b) No, they are used for debugging purposes
    • c) Yes, they can raise custom exceptions
    • d) No, assertions only check conditions
  8. Which of the following is a valid assertion with a message?
    • a) assert condition, "Error message"
    • b) assert "Error message" condition
    • c) assert condition then "Error message"
    • d) assert condition message="Error"
  9. What happens when an assertion statement is included in production code?
    • a) It has no effect
    • b) It slows down the performance
    • c) It checks the condition and raises an error if it fails
    • d) It always raises an exception
  10. In which scenarios would you typically use assertions in Python?
    • a) To validate function arguments during development
    • b) To handle runtime exceptions
    • c) To enforce business logic in production code
    • d) To replace try-except blocks

Answers (Tabular Form)

Question No.Answer
1b
2a
3a
4c
5b
6a
7b
8b
9c
10a
11b
12b
13a
14d
15b
16b
17c
18a
19d
20b
21a
22a
23b
24a
25b
26b
27b
28a
29a
30a

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top