w3resource

PCEP Certification Practice: try-except Exception Handling

PCEP Certification Practice Test - Questions, Answers and Explanations

Here are 25 questions provide comprehensive coverage of the try-except structure in Python, including handling exceptions, using finally and else blocks, and managing multiple exceptions.

Question 1: Which of the following is the correct usage of the try-except block in Python?

  1. It is used to handle exceptions and prevent the program from terminating abruptly.
  2. It is used to skip parts of the code that the developer doesn't want to execute.
  3. It is used to define a function with exception handling.
  4. It is used to print output to the console.

Answer: A) It is used to handle exceptions and prevent the program from terminating abruptly.

Explanation: The try-except block is used to handle exceptions and allows the program to handle errors gracefully without terminating.

Question 2: Which of the following statements are true about the try-except block in Python? (Select all that apply.)

  • Code in the try block is always executed.
  • Code in the except block is executed only if an exception occurs.
  • If no exception occurs, the except block is skipped.
  • You can have multiple except blocks for different exceptions

Answer: B) Code in the except block is executed only if an exception occurs.
C) If no exception occurs, the except block is skipped.
D) You can have multiple except blocks for different exceptions.

Explanation: The code in the try block is executed, and if an exception occurs, the except block is executed. Multiple except blocks can be used to handle different exceptions.

Question 3: In Python, the _________ block is used to catch exceptions and handle them.

  1. try
  2. except
  3. finally

Answer: B) except

Explanation: The except block is used to catch and handle exceptions raised in the try block.

Question 4: In Python, the __________ block is used to execute code that may raise an exception, while the except block is used to handle the exception.

Answer: try

Explanation: The try block is where the code that may raise an exception is written, and the except block handles the exception if one is raised.

Question 5: Fill in the blank to handle the ZeroDivisionError exception in the following code:

try:
    result = 10 / 0
except __________:
    print("Division by zero error occurred")

Answer: except ZeroDivisionError:

Explanation: A ZeroDivisionError is raised when dividing by zero. The except ZeroDivisionError block will catch and handle this specific exception.

Question 6: Which of the following will correctly catch any type of exception in Python?

  1. except:
  2. try:
  3. except Exception:
  4. try-except:

Answer: C) except Exception:

Explanation: except Exception: catches all exceptions that are derived from the Exception class, which includes most standard exceptions in Python.

Question 7: Insert the correct try-except structure to catch any type of exception that occurs in the following code:

try:
    result = 10 / 0
# INSERT CODE HERE to catch all exceptions
    print("An error occurred")

Answer: except Exception:

Explanation: except Exception: catches any exception that is derived from the Exception class. In this case, it will catch the ZeroDivisionError.

Question 8: Which of the following are true about using multiple except blocks in Python? (Select all that apply.)

  1. You can handle different types of exceptions with different except blocks.
  2. The try block can only be followed by one except block.
  3. Python will execute the first except block that matches the exception type.
  4. It is possible to catch multiple exceptions in a single except block.

Answer: A) You can handle different types of exceptions with different except blocks.
C) Python will execute the first except block that matches the exception type.
D) It is possible to catch multiple exceptions in a single except block.

Explanation: Python allows multiple except blocks to handle different types of exceptions. Python will execute the first matching except block, and you can also catch multiple exceptions using a tuple.

Question 9: The _________ block is executed when an exception occurs in the try block, but no matching except block is found.

  • finally
  • else
  • None

Answer: A) finally

Explanation: If an exception occurs and no matching except block is found, the program will terminate, and no further code is executed unless a finally block is present.

Question 10: If an exception occurs and is not handled by an except block, Python will terminate the program and display an __________.

Answer: error message

Explanation: If an exception is not caught by an except block, Python will terminate the program and print an error message, displaying the type of exception and the traceback.

Question 11: Arrange the following components of the try-except structure in the correct order of execution.

  1. except block
  2. Code after try-except
  3. try block

Answer: C) try block
A) except block
B) Code after try-except

Explanation: First, the try block is executed. If an exception occurs, the except block runs. After handling the exception, the remaining code is executed.

Question 12: Which of the following are correct ways to handle exceptions in Python? (Select all that apply.)

  1. Using try and except blocks.
  2. Using try without an except block.
  3. Handling specific exceptions like ZeroDivisionError in the except block.
  4. Using multiple except blocks for different exceptions.

Answer: A) Using try and except blocks.
C) Handling specific exceptions like ZeroDivisionError in the except block.
D) Using multiple except blocks for different exceptions.

Explanation: A try block must always be followed by one or more except blocks, or a finally block. Specific exceptions can be handled in the except block, and multiple except blocks can be used to handle different exceptions.

Question 13: The _________ block can be used to execute code whether an exception occurs or not.

  1. try
  2. except
  3. finally

Answer: C) finally

Explanation: The finally block executes regardless of whether an exception occurs or not, typically used for cleanup tasks.

Question 14: To handle different types of exceptions separately, you can use multiple __________ blocks in Python.

Answer: multiple

Explanation: You can use multiple except blocks to handle different types of exceptions separately, such as handling ZeroDivisionError, ValueError, etc.

Question 15: Fill in the blank to handle both ZeroDivisionError and ValueError in the same except block:

try:
    result = int(input("Enter a number: ")) / 0
except (__________ , ValueError):
    print("An error occurred")

Answer: except (ZeroDivisionError, ValueError):

Explanation: You can handle multiple exceptions in the same except block by specifying them as a tuple, separated by commas.

Question 16: What happens when an exception is raised in a try block but there is no matching except block?

  1. The program skips the try block and continues executing.
  2. The program raises the exception and stops.
  3. The program raises a SyntaxError.
  4. The program moves to the next try-except block.

Answer: B) The program raises the exception and stops

Explanation: If no matching except block is found, the program raises the exception and stops execution.

Question 17: Insert the correct except block to catch both ZeroDivisionError and TypeError exceptions:

try:
    result = "string" / 0
except __________:
    print("An error occurred")

Answer: except (ZeroDivisionError, TypeError):

Explanation: The except (ZeroDivisionError, TypeError) block catches both ZeroDivisionError and TypeError exceptions raised in the try block.

Question 18: Which of the following are valid except block structures? (Select all that apply.)

  1. except ValueError:
  2. except (ZeroDivisionError, ValueError):
  3. except:
  4. except (ValueError) as e:

Answer: A) except ValueError:
B) except (ZeroDivisionError, ValueError):
C) except:

Explanation: All of the options except D are valid. You can catch specific exceptions, multiple exceptions using a tuple, or all exceptions using a bare except: clause. Option D is invalid because the parentheses around ValueError are unnecessary when using the as e syntax.

Question 19: In Python, the _________ block runs if no exception is raised in the try block.

  1. finally
  2. else
  3. except

Answer: B) else

Explanation: The else block runs if no exceptions are raised in the try block, and it allows you to execute code only if the try block is successful.

Question 20: You can use the __________ block after the try and except blocks to ensure some code always runs, regardless of whether an exception occurred.

Answer: finally

Explanation: The finally block ensures that code within it always executes, regardless of whether an exception occurred in the try block.

Question 21: Sort the following components of the try-except block in the correct order of appearance.

  1. except
  2. try
  3. finally

Answer: B) try
A) except
C) finally

Explanation: The try block appears first, followed by one or more except blocks to handle exceptions. The finally block is optional and appears last.

Question 22: Which of the following are valid uses of the try-except block? (Select all that apply.)

  1. To handle specific exceptions such as ZeroDivisionError.
  2. To handle all exceptions using except:.
  3. To execute code whether an exception occurs or not using the else block.
  4. To run code regardless of whether an exception occurred using finally.

Answer: A) To handle specific exceptions such as ZeroDivisionError.
B) To handle all exceptions using except:.
D) To run code regardless of whether an exception occurred using finally.

Explanation: The try-except block can handle specific or general exceptions, and the finally block ensures that code runs whether an exception occurs or not. The else block runs only if no exception is raised.

Question 23: The exception _________ is raised when an invalid value is passed to a function that cannot handle it, and it can be caught using an except block.

  1. ValueError
  2. TypeError
  3. IndexError

Answer: A) ValueError

Explanation: A ValueError is raised when a function receives an argument of the correct type but with an invalid value, such as converting a non-numeric string to an integer.

Question 24: Fill in the blank to catch all exceptions that occur in the following code.

try:
    result = 10 / 0
except __________:
    print("An error occurred")

Answer: except Exception:

Explanation: except Exception: catches all exceptions that are subclasses of Exception, which includes most standard exceptions like ZeroDivisionError

Question 25: Which of the following blocks runs regardless of whether an exception is raised or not?

  1. try
  2. except
  3. else
  4. finally

Answer: D) finally

Explanation: The finally block always runs, regardless of whether an exception occurred or not, making it useful for cleanup operations like closing files or releasing resources.



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-try-except-exception.php