Courseiva

PCEP Computer Programming and Python Fundamentals Practice Question

A developer writes:

try:

x = int('hello') except ValueError: x = 0 except TypeError: x = -1 finally: x = x + 1 What is the final value of x?

⚠ Common exam trap

The PCEP exam often tests the misconception that the finally block does not execute when an exception is caught, or that the exception type might be misidentified, leading candidates to pick the value from an unexecuted except clause.

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

✓

1

The code attempts to convert the string 'hello' to an integer, which raises a ValueError. The except ValueError block catches this and sets x = 0. The finally block always executes, adding 1 to x, so x becomes 1. The TypeError except is never triggered because the error is a ValueError, not a TypeError.

Answer analysis

Option-by-option breakdown

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

  • ✗

    0

    Why it's wrong here

    The ValueError handler sets x to 0, then finally executes x = x + 1, overwriting it with 1. Choosing 0 ignores the finally block, which always runs regardless of whether an exception was caught. The TypeError handler never fires because int('hello') raises ValueError, not TypeError.

  • ✓

    1

    Why this is correct

    Converting `'hello'` raises `ValueError`, so the handler sets x to 0. The `TypeError` clause never executes. The `finally` block always runs, adding 1, giving x = 1. This satisfies the stem's requirement to trace exception handling and finalisation order precisely.

  • ✗

    -1

    Why it's wrong here

    The TypeError handler never executes, since int('hello') raises ValueError, so x is set to 0 by the matching handler. The finally block then increments it to 1. Selecting -1 assumes the wrong exception type is raised, which misreads how Python matches except clauses.

  • ✗

    2

    Why it's wrong here

    The ValueError handler assigns 0, and finally adds 1, giving 1. Reaching 2 would require the finally block to run twice or an initial value of 1, neither of which occurs. The TypeError handler is skipped because int('hello') raises ValueError.

About these practice questions

One of 482 original PCEP practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. 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.