Bird
0
0

What will happen when this code runs?

medium📝 Predict Output Q5 of 15
Python - Advanced Exception Handling
What will happen when this code runs?
def check(num):
    assert num != 0, 'Number cannot be zero'
    return 10 / num

print(check(0))
ARaises AssertionError with message 'Number cannot be zero'
BPrints 10
CRaises ZeroDivisionError
DPrints 0
Step-by-Step Solution
Solution:
  1. Step 1: Evaluate assert condition with num=0

    Condition num != 0 is false, so assert raises an error.
  2. Step 2: Identify error type and message

    AssertionError is raised with message 'Number cannot be zero'.
  3. Final Answer:

    Raises AssertionError with message 'Number cannot be zero' -> Option A
  4. Quick Check:

    Assert false = AssertionError [OK]
Quick Trick: Assert stops code before division by zero [OK]
Common Mistakes:
  • Expecting ZeroDivisionError instead of AssertionError
  • Ignoring assert message
  • Assuming code continues after assert failure

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes