w3resource

PCEP Certification Practice: Ordering except branches in Python

PCEP Certification Practice Test - Questions, Answers and Explanations

Here are 25 PCEP-style questions focused on “ordering the except branches” in Python, covering how exceptions are handled and the importance of properly ordering multiple except blocks. The questions follow the format you requested, using different types like single-select and multiple-select, "Place" & "Gap fill", sorting, code fill, and code insertion.

Question 1: What is the correct order for multiple except branches when handling exceptions in Python?

  1. More specific exceptions should come before more general exceptions.
  2. The order of except branches doesn't matter.
  3. More general exceptions should come before more specific exceptions.
  4. Exceptions should be ordered alphabetically.

Answer: A) More specific exceptions should come before more general exceptions.

Explanation: In Python, more specific exceptions should always be placed before more general exceptions, as the first matching except block will handle the exception.

Question 2: Which of the following are correct practices for ordering except branches in Python? (Select all that apply.)

  • Place except Exception: after more specific exceptions like ZeroDivisionError.
  • More specific exceptions should be handled before more general ones.
  • General exceptions should be placed before specific exceptions.
  • The order of except blocks is not important.

Answer: A) Place except Exception: after more specific exceptions like ZeroDivisionError.
B) More specific exceptions should be handled before more general ones.

Explanation: Specific exceptions must be handled before general ones. If except Exception: is placed before specific exceptions, it will catch all exceptions, preventing more specific handlers from being executed.

Question 3: In Python, except ___________: should come before except Exception: when ordering except branches.

  1. ValueError
  2. KeyError
  3. Both of the above

Answer: C) Both of the above

Explanation: ValueError and KeyError are more specific exceptions compared to Exception, so they should come before except Exception: to allow the specific exceptions to be handled separately.

Question 4: More specific exceptions like IndexError should come before general exceptions like __________ in the except branches.

Answer: Exception

Explanation: IndexError is a specific exception that should be caught before the more general Exception, as Exception would catch all errors if placed first.

Question 5: Fill in the blank to correctly order the except branches for handling specific exceptions before general exceptions:

try:
    result = int(input("Enter a number: ")) / 0
except __________:
    print("Division by zero!")
except Exception:
    print("An error occurred")

Answer: except ZeroDivisionError:

Explanation: The specific ZeroDivisionError is handled before the more general Exception. If Exception came first, it would catch all errors and prevent the ZeroDivisionError from being specifically handled.

Question 6: What happens if a more general except block is placed before a more specific one?

  1. Python will raise a SyntaxError.
  2. Python will raise a TypeError.
  3. The more general except block will catch all exceptions, and the specific ones will never be executed.
  4. Python will execute all except blocks in order.

Answer: C) The more general except block will catch all exceptions, and the specific ones will never be executed.

Explanation: If a general except block is placed before a specific one, it will catch all exceptions, and the specific except blocks will not be reached.

Question 7: Insert the correct specific exception before the general Exception in the following code:

try:
    result = int(input("Enter a number: "))
except __________:
    print("Invalid number!")
except Exception:
    print("An error occurred")

Answer: except ValueError:

Explanation: ValueError is a specific exception that occurs when int() is given an invalid input, and it should be handled before the general Exception.

Question 8: Which of the following statements are correct regarding exception ordering? (Select all that apply.)

  1. except Exception: should come last when ordering except branches.
  2. Placing except Exception: before more specific exceptions will prevent those specific exceptions from being handled.
  3. except Exception: should always be placed before except ValueError:.
  4. All except blocks should be ordered based on specificity, from most specific to most general.

Answer: A) except Exception: should come last when ordering except branches.
B) Placing except Exception: before more specific exceptions will prevent those specific exceptions from being handled.
D) All except blocks should be ordered based on specificity, from most specific to most general.

Explanation: except Exception: is a general handler that should be placed after more specific exceptions to allow those specific exceptions to be handled first.

Question 9: In Python, the _________ exception is more general than ValueError and should come after it in the except block order.

  • TypeError
  • Exception
  • KeyError

Answer: B) Exception

Explanation: Exception is more general than ValueError and should come after it when ordering except blocks.

Question 10: In an exception handling structure, the most general exception should be placed at the end, such as __________, which catches almost all exceptions.

Answer: Exception

Explanation: Exception is the most general exception class in Python, and it should be placed last to allow more specific exceptions to be caught first.

Question 11: Arrange the following exceptions in the correct order of specificity, from most specific to most general.

  1. ValueError
  2. Exception
  3. ZeroDivisionError

Answer: C) ZeroDivisionError
A) ValueError
B) Exception

Explanation: ZeroDivisionError is the most specific, followed by ValueError. Exception is the most general and should be placed last.

Question 12: Which of the following exceptions are more specific than Exception and should be handled before it? (Select all that apply.)

  1. KeyError
  2. ValueError
  3. TypeError
  4. SystemExit

Answer: A) KeyError
B) ValueError
C) TypeError

Explanation: KeyError, ValueError, and TypeError are all subclasses of Exception and are more specific exceptions. SystemExit is not a subclass of Exception; it is a subclass of BaseException.

Question 13: In a series of except blocks, the _________ block should be placed last as it will catch almost all exceptions.

  1. finally
  2. except Exception:
  3. else

Answer: B) except Exception:

Explanation: except Exception: should be placed last because it is a general handler that will catch almost any exception.

Question 14: The except ___________: block should be placed after all more specific exceptions like ValueError and TypeError to ensure correct exception handling.

Answer: Exception

Explanation: except Exception: should come last to handle general exceptions that are not covered by the more specific except blocks.

Question 15: Fill in the blank to order the except branches correctly, with the specific exception TypeError coming before the general exception:

try:
    result = "text" + 5
except __________:
    print("Type error!")
except Exception:
    print("Some other error occurred")

Answer: except TypeError:

Explanation: TypeError is a specific exception and should be handled before the general Exception. If Exception came first, it would prevent the TypeError from being specifically handled.

Question 16: What happens if an except Exception: block is placed before except ValueError:?

  1. Both except blocks will execute if a ValueError occurs.
  2. The except Exception: block will catch the ValueError, and except ValueError: will not execute.
  3. Python will raise a SyntaxError.
  4. The except ValueError: block will execute first.

Answer: B) The except Exception: block will catch the ValueError, and except ValueError: will not execute.

Explanation: The more general except Exception: will catch the ValueError, and the specific except ValueError: block will not be executed.

Question 17: Insert the correct code to catch a ValueError before catching the general Exception in the following code:

try:
    result = int("text")
except __________:
    print("Value error!")
except Exception:
    print("An error occurred")

Answer: except ValueError:

Explanation: ValueError should be handled before Exception, as it is more specific and can catch the error when trying to convert "text" to an integer.

Question 18: Which of the following practices should be followed when ordering except branches? (Select all that apply.)

  1. Place more specific exceptions like KeyError before general ones like Exception.
  2. Always catch specific exceptions before general ones.
  3. Place except Exception: at the start of the except block sequence.
  4. General exceptions should be placed last in the except block sequence.

Answer: A) Place more specific exceptions like KeyError before general ones like Exception.
B) Always catch specific exceptions before general ones.
D) General exceptions should be placed last in the except block sequence.

Explanation: Specific exceptions like KeyError should be placed before general ones like Exception. General exceptions should always come last to ensure that more specific ones are handled properly.

Question 19: In Python, the _________ exception is more general than TypeError and should come after it in the except block order.

  1. ValueError
  2. Exception
  3. KeyError

Answer: B) Exception

Explanation: Exception is more general than TypeError and should come after it when ordering except blocks.

Question 20: In a multi-branch except structure, the general except __________: block must be placed after specific exceptions to ensure the specific ones are handled first.

Answer: Exception

Explanation: except Exception: is a general exception handler and must be placed after specific exceptions like ValueError or TypeError to ensure they are handled first.

Question 21: Sort the following exceptions from most specific to most general in the Python exception hierarchy.

  1. KeyError
  2. ValueError
  3. Exception

Answer: A) KeyError
B) ValueError
C) Exception

Explanation: KeyError and ValueError are more specific than Exception, which is the most general exception and should be placed last.

Question 22: Which of the following exceptions should come before except Exception: when ordering except branches? (Select all that apply.)

  1. IndexError
  2. TypeError
  3. KeyError
  4. SystemExit

Answer: A) IndexError
B) TypeError
C) KeyError

Explanation: IndexError, TypeError, and KeyError are all more specific than Exception and should come before it. SystemExit is not caught by except Exception: and is a subclass of BaseException.

Question 23: The exception _________ should be placed after ValueError when handling exceptions in a multi-branch except structure.

  1. KeyError
  2. Exception
  3. TypeError

Answer: B) Exception

Explanation: Exception is more general than ValueError and should be placed after it when ordering except blocks.

Question 24: Fill in the blank to correctly order the except branches with the general exception last:

try:
    result = my_dict["missing_key"]
except __________:
    print("Key not found!")
except Exception:
    print("An error occurred")

Answer: except KeyError:

Explanation: KeyError is a specific exception and should come before the more general Exception.

Question 25: Which exception should be placed last in a multi-branch except structure?

  1. IndexError
  2. ValueError
  3. Exception
  4. TypeError

Answer: C) Exception

Explanation: Exception is the most general exception and should be placed last in a multi-branch except structure to ensure that more specific exceptions are handled first.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/python/certificate/functions-and-exceptions-ordering-the-except-branches.php