Bird
Raised Fist0

What will be the output of this code?

medium📝 Predict Output Q4 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 at least 18')
    return 'Access granted'

print(check_age(16))
AAccess granted
BValueError: Age must be at least 18
CTypeError: unsupported operand
DNone
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the function behavior

    If age is less than 18, the function raises a ValueError with a message.
  2. Step 2: Check the input value

    Input is 16, which is less than 18, so the error is raised.
  3. Final Answer:

    ValueError: Age must be at least 18 -> Option B
  4. Quick Check:

    Raise error triggers exception with message [OK]
Quick Trick: Input below condition triggers custom error [OK]
Common Mistakes:
MISTAKES
  • Expecting normal return instead of error
  • Confusing error type with TypeError
  • Ignoring the raise statement effect

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes