Courseiva
Exceptions and File I/O →hardMultiple Choice

PCAP Exceptions and File I/O Practice Question

Consider the following code snippet:

try:

x = int(input()) y = 10 / x

print(y)

except ZeroDivisionError:

print('Division by zero')

except ValueError:

print('Invalid integer')

If the user enters '0', what is the output?

⚠ Common exam trap

Python Institute often tests the order of exception handling and the fact that int('0') succeeds, leading candidates to mistakenly think a ValueError occurs or that both exceptions could fire.

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

✓

Division by zero

When the user enters '0', the input is successfully converted to the integer 0 by int(), so no ValueError occurs. Then 10 / 0 raises a ZeroDivisionError, which is caught by the except ZeroDivisionError block, printing 'Division by zero'. Option C is correct because the code never reaches the ValueError handler.

Answer analysis

Option-by-option breakdown

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

  • ✗

    No output

    Why it's wrong here

    Even if the input is '0', int('0') succeeds, so no conversion error occurs; the subsequent division 10 / 0 raises ZeroDivisionError, which is caught and prints 'Division by zero'. Since the except clauses are the only print statements, and one of them always executes for any input, the program cannot produce no output. A silent exit would require all exceptions to be unhandled, causing a traceback instead.

  • ✗

    Both 'Invalid integer' and 'Division by zero'

    Why it's wrong here

    After int() successfully converts the input '0' to the integer 0, the division operation raises ZeroDivisionError, and control transfers immediately to that handler. Python's exception handling is exclusive: only the first matching except block executes, and once it runs, the remaining except clauses are skipped. Thus 'Invalid integer' is impossible because a ValueError was never raised, and the two messages cannot be printed in the same run.

  • ✓

    Division by zero

    Why this is correct

    When the user enters '0', int('0') returns 0 without error, so the try block proceeds to evaluate 10 / 0. This expression raises ZeroDivisionError, and Python matches it to the except ZeroDivisionError clause, causing 'Division by zero' to be printed. The output is exactly that message, demonstrating that an exception raised by an arithmetic operation is handled by its specific handler.

  • ✗

    Invalid integer

    Why it's wrong here

    A ValueError occurs only when int() encounters a string that does not represent a valid integer, such as 'abc' or '12.5'. The string '0' is a valid decimal integer literal, so int('0') succeeds and returns 0, meaning the ValueError handler is never entered. Consequently, the program never prints 'Invalid integer'; instead, it continues to the division and fails later with a different exception type.

About these practice questions

Courseiva writes every PCAP question from scratch — 421 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 PCAP 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 PCAP exam.