PCEP Practice Question: Functions, Tuples, Dictionaries and Exceptions
What is the output of the following code?
def div(a, b):
try:
return a / bexcept ZeroDivisionError: raise ValueError('Invalid division')
try:
print(div(10, 0))except ValueError as e:
print(e)
except ZeroDivisionError:
print('Zero division')⚠ Common exam trap
The PCEP exam often tests the distinction between catching an exception and raising a different exception inside the handler, tricking candidates into thinking the original exception type (ZeroDivisionError) will still be caught by the outer handler.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
Invalid division
The `div` function catches the `ZeroDivisionError` and raises a `ValueError` with the message 'Invalid division'. The outer `try` block catches this `ValueError` and prints its message, which is 'Invalid division'.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Error: division by zero
Why it's wrong here
The original ZeroDivisionError is not propagated.
- ✗
Zero division
Why it's wrong here
Incorrect; the ZeroDivisionError is caught inside div and converted to ValueError.
- ✗
TypeError
Why it's wrong here
No type error occurs.
- ✓
Invalid division
Why this is correct
Correct; the ValueError is raised and caught.
Go deeper
Related to this question
About these practice questions
This PCEP question is part of Courseiva's 498-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →
Same concept, more angles
4 more ways this is tested on PCEP
These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.
Variation 1. What is the output when the following code is executed? try: print(1/0) except ZeroDivisionError: print("Cannot divide by zero")
medium- ✓ A.Cannot divide by zero
- B.The program crashes with an unhandled exception
- C.Result: 0
- D.Unsupported operand
Why A: The code inside the try block attempts to divide by zero, which raises a ZeroDivisionError. The except block catches this exception and executes its print statement, outputting 'Cannot divide by zero'.
Variation 2. Refer to the exhibit. What type of error occurred, and which line caused it?
medium- A.NameError at line 3
- B.SyntaxError at line 1
- C.ValueError at line 0
- ✓ D.ZeroDivisionError at line 3
Why D: The code attempts to divide by zero on line 3, which raises a ZeroDivisionError. In Python, any division where the denominator is zero (e.g., `x / 0`) triggers this runtime exception, and the traceback points to the exact line where the division occurs.
Variation 3. What is the output of the following code? try: print(1/0); except ZeroDivisionError: print('error'); finally: print('done')
hard- ✓ A.error done
- B.error
- C.done error
- D.1/0 done
Why A: The code attempts to divide 1 by 0, which raises a ZeroDivisionError. The except block catches this specific exception and prints 'error', then the finally block always executes, printing 'done'. The output is therefore 'error' followed by 'done' on separate lines.
Variation 4. Based on the exhibit, where did the exception originate?
hard- ✓ A.At line 5 in the divide function inside app.py.
- B.In the main module outside any function.
- C.In the ZeroDivisionError exception handler.
- D.At line 10 in app.py, where the function was called.
Why A: The exception (ZeroDivisionError) originates at line 5 inside the divide function in app.py, where the code attempts to divide by zero. The traceback shows the innermost frame first, indicating the exact line where the error was raised.
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This PCEP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCEP exam.