Bird
0
0

Consider this code:

hard📝 Application Q9 of 15
Python - Exception Handling Fundamentals
Consider this code:
def safe_divide(a, b):
    try:
        return a / b
    except ZeroDivisionError:
        return 'Cannot divide by zero'
    except TypeError:
        return 'Invalid input type'

What will safe_divide('10', 2) return?
A'Cannot divide by zero'
B'Invalid input type'
C5.0
DTypeError exception raised
Step-by-Step Solution
Solution:
  1. Step 1: Analyze function call

    Calling safe_divide('10', 2) tries to divide string by int, which raises TypeError.
  2. Step 2: Check exception handling

    The except block for TypeError returns 'Invalid input type'.
  3. Final Answer:

    'Invalid input type' -> Option B
  4. Quick Check:

    TypeError caught returns message [OK]
Quick Trick: TypeError occurs when dividing incompatible types [OK]
Common Mistakes:
  • Expecting division result
  • Confusing with ZeroDivisionError
  • Assuming exception is raised uncaught

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes