Python Exception Handling: try, except, else & finally
1. What is the output? try: print(10 / 0) except ZeroDivisionError: print("Error")
A. 0
B. Nothing
C. Error
D. 10
2. What is the output? try: x = 10 except: print("Error") else: print("Success")
A. Error
B. 10
C. Success
D. Nothing
3. What is the output? try: print(5 / 0) except ZeroDivisionError: print("Caught") finally: print("Done")
A. Caught
B. Done
C. Error\nDone
D. Caught\nDone
4. What is the output? try: print("A") except: print("B") finally: print("C")
A. C\nA
B. A
C. A\nC
D. B\nC
5. Which block is designed to execute whether or not an exception occurs? try: # risky operation except Exception: # handle error else: # no error finally: # cleanup
A. except
B. try
C. finally
D. else
6. What is the output? try: int("abc") except ValueError: print("Invalid") else: print("Valid") finally: print("Finished")
A. Invalid
B. Invalid\nFinished
C. Valid\nFinished
D. Finished\nInvalid
7. What happens to the else block when the try block raises an exception that is handled by except?
A. It executes before except
B. It is skipped
C. It always executes
D. It executes after finally
8. What is the output? try: x = 10 print(x) except Exception: print("Error") finally: print("Cleanup")
A. Error\nCleanup
B. 10\nCleanup
C. Cleanup\n10
D. 10
9. What is the main purpose of the else block in Python exception handling?
A. Run code only when try fails
B. Run code only when try succeeds
C. Always clean up resources
D. Catch every exception
10. What is the output? try: print("Start") raise Exception("Oops") except Exception: print("Handled") finally: print("End")
A. Handled\nEnd
B. Start\nEnd
C. Start\nHandled
D. Start\nHandled\nEnd