w3resource

PCEP Certification Practice: Exception Propagation through Function Boundaries

PCEP Certification Practice Test - Questions, Answers and Explanations

Here are 25 PCEP-style questions based on propagating exceptions through function boundaries in Python. These questions follow the requested format, using various types like single and multiple-select, "Place" & "Gap fill", sorting, code fill, and code insertion.

Question 1: What happens when an exception occurs in a function and it is not handled within that function?

  1. The exception is ignored.
  2. The exception is propagated to the calling function.
  3. The function automatically returns None.
  4. The program enters an infinite loop.

Answer: B) The exception is propagated to the calling function.

Explanation: If an exception is not handled within a function, it is propagated to the calling function. If it is not caught there, it continues propagating until it is caught or reaches the top level, which may terminate the program.

Question 2: Which of the following are true about exceptions propagating through function boundaries?

  • If an exception occurs in a function and is not caught, it will propagate to the calling function.
  • If an exception is caught in a function, it will not propagate to the calling function.
  • Uncaught exceptions terminate the program if they propagate to the top level.
  • Exceptions must always be caught within the same function they occur in.

Answer: A) If an exception occurs in a function and is not caught, it will propagate to the calling function.
B) If an exception is caught in a function, it will not propagate to the calling function.
C) Uncaught exceptions terminate the program if they propagate to the top level.

Explanation: Exceptions that are not caught in a function propagate to the calling function. If they are caught, they stop propagating. If uncaught exceptions reach the top level of the program, they will cause the program to terminate.

Question 3: In Python, an uncaught exception in a function will propagate to the _________.

  1. calling function
  2. exception block
  3. finally block

Answer: A) calling function

Explanation: An uncaught exception in a function will propagate to the function that called it. If it is not caught there, it will continue propagating.

Question 4: If a function does not catch an exception, it is automatically propagated to the __________.

Answer: calling function

Explanation: When an exception is raised in a function and not caught, it is propagated to the calling function, where it can be handled or continue propagating.

Question 5: Fill in the blanks to catch the ZeroDivisionError in the calling function and not within the divide() function:

def divide(a, b):
    result = a / b
    return result

try:
    divide(10, 0)
except __________:
    print("Division by zero!")

Answer: except ZeroDivisionError:

Explanation: The exception is propagated from the divide() function to the calling code. The ZeroDivisionError is caught and handled in the try-except block outside the function.

Question 6: What happens if a function raises an exception that is not handled inside the function or the calling function?

  1. The exception is ignored.
  2. The program terminates.
  3. The exception is automatically caught at the top level.
  4. The exception causes an infinite loop.

Answer: B) The program terminates.

Explanation: If an exception is not caught within the function or the calling function, it propagates up to the top level. If it is still uncaught, the program terminates.

Question 7: Insert the correct except block to handle an exception propagated from a function:

def raise_exception():
    raise ValueError("An error occurred")

try:
    raise_exception()
# INSERT CODE HERE
    print("Error handled")

Answer: except ValueError:

Explanation: The raise_exception() function raises a ValueError, which is propagated to the calling code. The except ValueError block catches and handles this exception.

Question 8: Which of the following are correct regarding exception propagation in Python? (Select all that apply.)

  1. Exceptions propagate from a function to the code that called it if not caught.
  2. If a function raises an exception and catches it, the exception will still propagate.
  3. Uncaught exceptions can propagate through multiple function calls.
  4. An exception caught in a function does not propagate to the caller.

Answer: A) Exceptions propagate from a function to the code that called it if not caught.
C) Uncaught exceptions can propagate through multiple function calls.
D) An exception caught in a function does not propagate to the caller.

Explanation: Uncaught exceptions propagate from a function to its caller and can continue propagating through multiple levels. If an exception is caught, it stops propagating.

Question 9: When an exception is not caught within a function, it is propagated to the _________.

  • next line
  • function that called it
  • finally block

Answer: B) function that called it

Explanation: When an exception is not caught in a function, it is propagated to the function that called it, allowing the caller to handle the exception.

Question 10: If a function raises an exception and does not catch it, the exception will propagate to the __________ function.

Answer: calling

Explanation: If a function raises an exception and it is not caught, it will propagate to the calling function, where it can be handled or continue propagating.

Question 11: Sort the following steps in the order in which an uncaught exception propagates through function calls.

  1. Function where the exception occurred.
  2. Function that called the function where the exception occurred.
  3. Main program that called the top-level function.

Answer: A) Function where the exception occurred.
B) Function that called the function where the exception occurred.
C) Main program that called the top-level function.

Explanation: An uncaught exception first propagates from the function where it occurred, then to the calling function, and finally to the top level (main program).

Question 12: Which of the following are consequences of not handling exceptions in a function? (Select all that apply.)

  1. The exception is propagated to the calling function.
  2. The program will terminate if the exception is not caught at any level.
  3. The exception will automatically be handled by Python.
  4. The exception may propagate through multiple layers of function calls.

Answer: A) The exception is propagated to the calling function.
B) The program will terminate if the exception is not caught at any level.
D) The exception may propagate through multiple layers of function calls.

Explanation: If an exception is not caught within a function, it propagates to the calling function. If it remains uncaught, it can propagate through multiple layers, eventually terminating the program if not handled.

Question 13: In Python, exceptions can propagate through multiple function calls and can be caught by the _________ function that calls them.

  1. first
  2. last
  3. any

Answer: A) first

Explanation: In Python, exceptions can propagate through multiple function calls and can be caught by the first function that calls them.

Question 14: If an exception is not caught by a function, it propagates to the calling function, and if still uncaught, it reaches the __________ level.

Answer: top

Explanation: If an exception is not caught, it propagates to the calling function and eventually reaches the top level, where it will terminate the program if uncaught.

Question 15: Fill in the blank to propagate the exception from inner_function() to outer_function():

def inner_function():
    raise KeyError("Key not found")

def outer_function():
    try:
        inner_function()
    except __________:
        print("Key error caught in outer function")

outer_function()

Answer: except KeyError:

Explanation: The exception raised in inner_function() propagates to outer_function(), where it is caught by the except KeyError block.

Question 16: If a function raises an exception and the calling function catches it, what happens?

  1. The exception is propagated further.
  2. The program terminates.
  3. The exception is handled, and the program continues.
  4. The function where the exception occurred is executed again.

Answer: C) The exception is handled, and the program continues.

Explanation: If the calling function catches the exception, it is handled, and the program continues execution.

Question 17: Insert the correct code to handle an exception that is propagated through multiple function calls:

def func_a():
    raise ValueError("An error occurred")

def func_b():
    func_a()

def func_c():
    try:
        func_b()
# INSERT CODE HERE to catch the exception
        print("Error handled in func_c")

func_c()

Answer: except ValueError:

Explanation: The ValueError raised in func_a() propagates through func_b() and is caught by the except ValueError block in func_c().

Question 18: Which of the following statements are true about exceptions propagating through multiple functions? (Select all that apply.)

  1. The function where the exception occurred must catch it.
  2. The calling function can catch an exception raised in the called function.
  3. An exception can propagate through several function calls before being caught.
  4. The exception must be caught in the same function where it occurs.

Answer: B) The calling function can catch an exception raised in the called function.
C) An exception can propagate through several function calls before being caught.

Explanation: Exceptions do not need to be caught in the same function where they occur. They can propagate through multiple function calls and be caught by any calling function.

Question 19: An exception can propagate through multiple function calls and can be caught by any function in the _________.

  1. call chain
  2. exception hierarchy
  3. input stream

Answer: A) call chain

Explanation: An exception can propagate through the chain of function calls (the call chain), and it can be caught by any function in that chain.

Question 20: When an exception is not caught in a function, it propagates through the __________ of function calls.

Answer: chain

Explanation: If an exception is not caught in a function, it propagates through the chain of function calls until it is caught or reaches the top level.

Question 21: Sort the following steps in the order in which an uncaught exception propagates through function calls.

  1. The function that called the function where the exception occurred.
  2. The function where the exception occurs.
  3. The main function or top-level code.

Answer: B) The function where the exception occurs.
A) The function that called the function where the exception occurred.
C) The main function or top-level code.

Explanation: An uncaught exception propagates from the function where it occurred, to the function that called it, and ultimately to the top-level code or main function.

Question 22: Which of the following functions can catch an exception propagated from a lower-level function? (Select all that apply.)

  1. The function where the exception occurred.
  2. Any function in the call chain.
  3. The main function or top-level code.
  4. Only the function where the exception occurred.

Answer: B) Any function in the call chain.
C) The main function or top-level code.

Explanation: Any function in the call chain, including the main function or top-level code, can catch an exception that is propagated from a lower-level function.

Question 23: In Python, an exception will propagate through the function call _________ if it is not caught.

  1. return value
  2. stack
  3. hierarchy

Answer: B) stack

Explanation: Exceptions propagate through the call stack. If an exception is not caught in a function, it is passed up the stack to the calling function.

Question 24: Fill in the blank to propagate the exception from inner_func() to outer_func():

def inner_func():
    raise TypeError("Type mismatch")

def outer_func():
    try:
        inner_func()
    except __________:
        print("TypeError caught in outer_func")

outer_func()

Answer: except TypeError:

Explanation: The TypeError raised in inner_func() propagates to outer_func(), where it is caught by the except TypeError block.

Question 25: What happens when an exception propagates through multiple function calls and reaches the top level of the program without being caught?

  1. The program terminates.
  2. The exception is ignored.
  3. The program continues running.
  4. The function where the exception occurred is called again.

Answer: A) The program terminates.

Explanation: If an exception propagates through multiple function calls and reaches the top level without being caught, the program will terminate.



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://www.w3resource.com/python/certificate/functions-and-exceptions-propagating-exceptions-through-function-boundaries.php