Courseiva

PCEP Practice Question: Functions, Tuples, Dictionaries and Exceptions

A developer writes a function that reads an integer from a string and adds it to a running total. The code uses try: total += int(value) followed by except ValueError: total += 0. During testing, a value of '12.5' is supplied. What is the resulting behaviour?

⚠ Common exam trap

Many exam-takers confuse int's behaviour on a float object, where it truncates, with its behaviour on a string such as '12.5', where it raises ValueError.

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

✓

The ValueError handler runs, total is incremented by 0, and execution continues normally.

int applied to the string '12.5' cannot produce an integer and raises ValueError. The except ValueError clause matches that exception type, so its body runs, adding zero to the total and allowing execution to continue. The conversion does not silently truncate string decimals, and the handler is correctly typed to catch exactly this failure.

Answer analysis

Option-by-option breakdown

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

  • ✗

    The program terminates with an unhandled ValueError because except clauses cannot add to variables defined outside the function.

    Why it's wrong here

    An except block is ordinary code and may modify names in its enclosing scope, including a running total, subject to the usual rules about rebinding. The ValueError from int('12.5') is matched by the handler, so it is handled, not unhandled. The claim that handlers cannot update outer variables is false and unrelated to exception matching.

  • ✗

    The conversion succeeds and total is increased by 12 because int truncates the decimal portion.

    Why it's wrong here

    int does not truncate strings containing decimals; it raises ValueError for '12.5' because the string is not a valid base-10 integer literal. Truncation toward zero applies when int receives a float object such as int(12.5), not when it parses a string. Therefore the conversion fails and the handler, not the success path, is what runs.

  • ✗

    A TypeError is raised because the except clause names ValueError rather than the broader Exception class.

    Why it's wrong here

    The exception raised by int('12.5') is ValueError, which exactly matches the named handler, so no mismatch occurs and no TypeError arises. Naming a specific exception class is the recommended practice; it does not cause a type error. The handler catches the failure and the program continues, adding zero to the total.

  • ✓

    The ValueError handler runs, total is incremented by 0, and execution continues normally.

    Why this is correct

    int('12.5') raises ValueError because the string contains a decimal point that int cannot parse. The except ValueError clause matches that exception, so its body executes and adds zero to the running total. Because the exception is handled, the program does not terminate; control resumes after the try-except statement, which is the intended graceful-degradation behaviour.

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 and reviewed by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

Last reviewed September 2026 · checked against the official Python Institute exam blueprint

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.