Bird
0
0

Find the error in this code:

medium📝 Debug Q6 of 15
Python - Advanced Exception Handling
Find the error in this code:
def validate(x):
    assert x > 10, 'x must be greater than 10'
    print('Valid')

validate(5)
ANo error, prints 'Valid'
BTypeError because of wrong assert usage
CSyntaxError due to assert statement
DAssertionError with message 'x must be greater than 10'
Step-by-Step Solution
Solution:
  1. Step 1: Check assert condition with x=5

    5 > 10 is false, so assert fails.
  2. Step 2: Identify error raised

    AssertionError is raised with the given message.
  3. Final Answer:

    AssertionError with message 'x must be greater than 10' -> Option D
  4. Quick Check:

    Assert false triggers AssertionError [OK]
Quick Trick: Assert fails if condition is false, raising error [OK]
Common Mistakes:
  • Expecting print output despite assert failure
  • Confusing AssertionError with SyntaxError
  • Thinking assert changes variable values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes