Courseiva
Exceptions and File I/O →easyMultiple Choice

PCAP Exceptions and File I/O Practice Question

What will be the output of the following code?

try:
        print(1/0)
    except:
        print('error')

else:

print('no error')

finally:

print('done')

⚠ Common exam trap

Python Institute often tests the order of execution in try/except/else/finally, specifically that the else block is skipped when an exception occurs, and that finally always runs, leading candidates to mistakenly include 'no error' or omit 'error'.

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

✓

error\ndone

The code raises a ZeroDivisionError when attempting 1/0, which is caught by the bare except clause, printing 'error'. The else clause is skipped because an exception occurred. The finally clause always executes, printing 'done'. Thus the output is 'error' followed by 'done'.

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 error\ndone

    Why it's wrong here

    The output 'no error\ndone' would be produced only if the try block completed without raising an exception, allowing the else clause to print 'no error' and the finally clause to print 'done'. However, an exception does occur, so execution jumps to the except block, which prints 'error', and the else clause is skipped entirely. The absence of an 'error' line is therefore incorrect.

  • ✗

    error\nno error\ndone

    Why it's wrong here

    This output implies that both the except clause and the else clause executed during the same run, printing 'error' and then 'no error' in sequence. In Python's exception-handling flow, else and except are mutually exclusive: if an exception occurs, except runs and else is bypassed; if no exception occurs, else runs and except is bypassed. Showing both from a single execution is impossible, making this option wrong.

  • ✗

    done

    Why it's wrong here

    Printing only 'done' would suggest that no exception was caught and no output came from the except block, with only the finally clause executing. In reality, the except block catches the raised exception and prints 'error' before the finally block runs. The finally clause always executes afterward, but it does not replace or suppress the except block's output, so the result must include 'error' followed by 'done'.

  • ✓

    error\ndone

    Why this is correct

    This is the correct output because the try block raises an exception that is caught by the except handler, which immediately prints 'error'. The else clause, which would print 'no error', is skipped because an exception was raised. The finally clause then unconditionally executes and prints 'done', producing exactly two lines: 'error' and 'done' in that order.

About these practice questions

One of 421 original PCAP 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 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.