Home › Interview Questions › What will be the output of the following Python sc…

What will be the output of the following Python script that demonstrates try, except, else, and finally blocks?

🟢 Easy Conceptual Junior level
1Times asked
Oct 2026Last seen
Oct 2026First seen

💡 Model Answer

Running the script produces two cases. When divide(10, 2) is called, the division succeeds, so the try block completes without raising an exception. The else block then prints 'Success: 10 / 2 = 5.0', and the finally block prints 'Finally: Always runs (cleanup)'. When divide(10, 0) is called, the division raises a ZeroDivisionError. Control jumps to the except ZeroDivisionError block, which prints 'Error: Cannot divide by zero.'. The else block is skipped because an exception occurred. Regardless, the finally block still executes, printing 'Finally: Always runs (cleanup)'. Thus the full output is: --- Case 1: Valid division --- Success: 10 / 2 = 5.0 Finally: Always runs (cleanup). --- Case 2: Division by zero --- Error: Cannot divide by zero. Finally: Always runs (cleanup). This illustrates that finally always runs, and else only runs when no exception occurs.

This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.

🎤 Get questions like this answered in real-time

Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers on a discreet on-screen overlay.

Get Assisting AI — Starts at ₹500