Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q13 of 15
Python - Advanced Exception Handling
What will be the output of this code?
def check_age(age):
    assert age >= 18, "Age must be at least 18"
    return "Access granted"

print(check_age(20))
print(check_age(16))
AAccess granted\nAccess granted
BAccess granted\nAssertionError: Age must be at least 18
CAssertionError: Age must be at least 18\nAccess granted
DNo output, program crashes immediately
Step-by-Step Solution
Solution:
  1. Step 1: Analyze first function call

    check_age(20) passes the assert since 20 >= 18, so it returns "Access granted" and prints it.
  2. Step 2: Analyze second function call

    check_age(16) fails the assert because 16 < 18, so it raises AssertionError with the message.
  3. Final Answer:

    Access granted\nAssertionError: Age must be at least 18 -> Option B
  4. Quick Check:

    Assert stops program on false condition with error [OK]
Quick Trick: Assert stops at first false condition with error [OK]
Common Mistakes:
  • Thinking both print statements run
  • Ignoring the error message
  • Assuming assert prints message without error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes