Exceptions & Files · Question 1 · Easy
A division by zero happens inside a try block that has a matching except clause. What is printed?
try:
print(10 // 0)
except ZeroDivisionError:
print("cannot divide")
- 0
- ZeroDivisionError
- 0 cannot divide
- cannot divide
Answer and explanation
Answer: D. cannot divide
Explanation: 10 // 0 raises ZeroDivisionError before print gets any value. The matching except block handles it and prints its message, and the program continues normally.
Next: Which exception does Python raise when int() is given text that is not a whole…
Practise exceptions & files
Try a fresh set of exceptions & files questions with an explanation after every answer, free and without signing up: Exceptions & Files practice. Or sit a full placement mock test.
More exceptions & files questions
- Which exception does Python raise when int() is given text that is not a whole…
- In a try statement, when does the code inside the finally block run?
- A dictionary of marks is asked for a name that was never stored. Which…
- A file report.txt already has some text in it. What happens to that text when…
- The try block below completes without any error and the statement has an else…
- io.StringIO lets a string be read like a text file. How many lines does…
- A try statement has except, else and finally clauses, and an error occurs in…
- An out-of-range list index is handled by two except clauses, the first naming…
- A custom exception carries a message built inside withdraw. What does printing…
- Lines of numbers are read from a file-like object inside a with block and…
More python practice
Built-ins & Standard Library · Variables, Types & Operators · Python Strings · Lists & Tuples · Dictionaries & Sets · Conditions & Loops · Functions & Scope · Comprehensions & Generators
LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge