What will be the output of the following code?try: print(1/0)except ZeroDivisionError: print("Cannot divide by zero")finally: print("Execution completed")
Question
What will be the output of the following code?try: print(1/0)except ZeroDivisionError: print("Cannot divide by zero")finally: print("Execution completed")
Solution
The output of the code will be:
Cannot divide by zero Execution completed
Explanation:
- The code first tries to execute the print(1/0) statement. Since division by zero is undefined in mathematics, Python raises a ZeroDivisionError.
- The except block catches this ZeroDivisionError and executes the print("Cannot divide by zero") statement. So, "Cannot divide by zero" is printed.
- The finally block executes regardless of whether an exception was raised or not. So, "Execution completed" is printed.
Similar Questions
29. What is the output of the following Python code?*try:result = 10 / 0except ZeroDivisionError:result = “Error: Division by zero”finally:print(result)
What will the following code raise? try: print(5/0) except ZeroDivisionError: print("Cannot divide by zero!")
class ZeroDenominatorError(ZeroDivisionError): pass try: a = 10 b = 5 if(b==0): raise ZeroDenominatorError() c = a/b except ZeroDivisionError: print('Zero Division Error occured',end= ‘ ‘) except ZeroDenominatorError: print('Zero Denominator Error occured',end = ‘ ‘) else: print(‘else works’)z = ZeroDenominatorError()Options: Pick one correct answer from belowZero Division Error occurred else worksZero Denominator Error occurred else workselse worksNone of the AbovePrevSubmit MCQNext
What will be the output of the following program in Python?print(0 or 1)
What will be the output for the following code snippet if a value is 8 and b value is xyz ?try: a=int(input()) b=int(input()) print(a//b)except ZeroDivisionError as e: print(e)except: print("Error")
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.