Bird
Raised Fist0

What will be the output of this code?

medium📝 Predict Output Q13 of Q15
Python - Advanced Exception Handling
What will be the output of this code?
def check_age(age):
    if age < 18:
        raise ValueError('Age must be 18 or older')
    return 'Access granted'

print(check_age(16))
AAccess granted
BNone
CValueError: Age must be 18 or older
DSyntaxError
Step-by-Step Solution
Solution:
  1. Step 1: Check the condition in the function

    The function raises a ValueError if age is less than 18. Here, age is 16, so the error triggers.
  2. Step 2: Understand what happens on raise

    When the error is raised, the program stops and shows the error message instead of returning 'Access granted'.
  3. Final Answer:

    ValueError: Age must be 18 or older -> Option C
  4. Quick Check:

    raise triggers error output = ValueError message [OK]
Quick Trick: If condition fails, raise stops and shows error message [OK]
Common Mistakes:
MISTAKES
  • Expecting function to return 'Access granted' anyway
  • Confusing error message with print output
  • Thinking raise prints message but continues

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes