Courseiva

PCEP Practice Question: Functions, Tuples, Dictionaries and Exceptions

What is the output of the following code?

def test():
    try:
        return 1

finally:

return 2

print(test())

⚠ Common exam trap

The PCEP exam often tests the misconception that a `try` block's return will take precedence over a `finally` block's return, leading candidates to pick option D (1) instead of understanding that `finally` overrides the return value.

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

✓

2

In Python, a `finally` block always executes, and if both `try` and `finally` contain `return` statements, the `return` in `finally` overrides the one in `try`. The function `test()` returns 2, not 1, because the `finally` block's return value is the one that is actually used.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✗

    None

    Why it's wrong here

    The function returns 2, not None, because the finally block's return statement overrides the try block's pending return value. It tempts because None is the default when a function lacks a return, yet both branches here contain explicit return statements, so None never applies.

  • ✗

    Error

    Why it's wrong here

    A finally block's return overrides the try block's pending return rather than raising, so no exception occurs and no error is printed. It tempts because finally is associated with cleanup during exceptions, but here it executes normally and simply replaces the return value with 2.

  • ✓

    2

    Why this is correct

    A `finally` block's `return` overrides any `return` in the `try` block, so the value 2 replaces the pending 1 before the function exits. Python discards the earlier return value once `finally` executes its own `return`, making 2 the printed output.

  • ✗

    1

    Why it's wrong here

    The try block's return 1 is discarded because a return inside finally overrides any pending return from try, so the function yields 2. It tempts because try appears to execute first, but Python defers the try return until finally completes, and finally's return wins.

About these practice questions

Courseiva writes every PCEP question from scratch — 482 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

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.