Exceptions & Files questions and answers

try/except/else/finally, raising errors and the with statement. Practise exceptions & files questions from the python section of placement tests, with a worked explanation after every answer. Practice is free and needs no sign-up.

Sample exceptions & files questions with worked answers

Question 1

In a try statement, when does the code inside the finally block run?

  1. Always, whether or not an exception was raised in the try block
  2. Only when the try block raises an exception
  3. Only when the try block finishes without an exception
  4. Only when no except clause matches the exception

Answer: A. Always, whether or not an exception was raised in the try block

Explanation: finally is meant for clean-up such as closing a file, so Python runs it on every path out of the try statement: normal completion, a handled exception or an unhandled one.

Question 2

Why is with open("data.txt") as f: preferred over calling open() and close() separately?

  1. The file is read faster because with loads it fully into memory
  2. Any exception raised inside the block is silently ignored
  3. The file is closed automatically when the block ends, even if an exception occurs inside it
  4. The file is created automatically if it does not exist

Answer: C. The file is closed automatically when the block ends, even if an exception occurs inside it

Explanation: The with statement calls the file's clean-up code on every exit from the block, including when an error is raised. It does not hide the error or change how the file is opened.

Question 3

Which of these exceptions is NOT caught by a handler written as except Exception:?

  1. ZeroDivisionError
  2. KeyError
  3. KeyboardInterrupt
  4. FileNotFoundError

Answer: C. KeyboardInterrupt

Explanation: KeyboardInterrupt derives directly from BaseException, not from Exception, so that Ctrl+C is not swallowed by ordinary handlers. The other three are all subclasses of Exception.

All 20 exceptions & files questions with answers

Exceptions & Files: common questions

What are exceptions & files questions?

try/except/else/finally, raising errors and the with statement. They appear in the python section of many placement tests.

Where can I practise exceptions & files questions with answers for free?

Right here on LogicWiz Exams. Every exceptions & files question has a worked explanation after you answer. Practice is free and needs no sign-up.

How should I practise exceptions & files?

Start with easy questions, then move to medium and hard. Read the explanation after each answer, and come back to the ones you missed.

Start practising

On this page you can start a fresh set of exceptions & files questions, choose easy, medium or hard, and time yourself if you want to. Or practise the whole subject: Python practice.

More python topics

Other subjects

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge · Test from a job description · LogicWiz Open: free team quiz · Free interview prep