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):
    if age < 18:
        raise ValueError("Too young")
    return "Access granted"

try:
    print(check_age(16))
except ValueError as e:
    print(e)
AToo young
BAccess granted
CValueError exception not caught
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Analyze function behavior with age 16

    Since 16 < 18, the function raises a ValueError with message "Too young".
  2. Step 2: Check exception handling in try-except

    The exception is caught by the except block, which prints the error message "Too young".
  3. Final Answer:

    Too young -> Option A
  4. Quick Check:

    Exception message printed = Too young [OK]
Quick Trick: Exception message prints if caught in except [OK]
Common Mistakes:
  • Assuming function returns 'Access granted'
  • Thinking exception crashes program
  • Missing that except prints the error message

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes