Courseiva

PCEP Practice Question: Functions, Tuples, Dictionaries and Exceptions

What does the following code output?

try:

x = int('abc') except ValueError:

print('Invalid')

⚠ Common exam trap

The PCEP exam often tests whether candidates understand that a caught exception does not crash the program; the trap here is that some candidates think any error causes a crash, but the except block prevents that.

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

The code attempts to convert the string 'abc' to an integer using int(). Since 'abc' is not a valid integer, Python raises a ValueError. The except block catches this specific exception and executes print('Invalid'), so the output is 'Invalid'. Option B is correct because the exception is handled gracefully without crashing.

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 crashes

    Why it's wrong here

    int('abc') raises ValueError, which the except clause catches and handles by printing 'Invalid', so execution continues normally rather than terminating. The option is tempting because an unhandled exception of that type would indeed crash the program, but here the matching handler prevents that outcome.

  • ✓

    Invalid

    Why this is correct

    Converting 'abc' with int() raises a ValueError, since the string holds no valid integer literal. The except clause names ValueError specifically, so it catches that exception and executes print('Invalid'). The try block aborts at the failing assignment, meaning nothing else runs before the handler outputs the required text.

  • ✗

    (Nothing printed)

    Why it's wrong here

    The except ValueError block executes and prints 'Invalid', so output is produced rather than suppressed. This is tempting because a try block that raises before any print would yield nothing if no handler matched, but the ValueError raised by int('abc') is caught, so the message appears.

  • ✗

    abc

    Why it's wrong here

    The string 'abc' is the input to int(), which raises ValueError before any assignment completes; the handler prints the literal 'Invalid', not the original text. Printing 'abc' would require an explicit print of the variable, which never receives a value here.

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.