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_positive(num):
    if num <= 0:
        raise ValueError('Number must be positive')
    return num

print(check_positive(-5))
ANumber must be positive
BNone
C-5
DValueError: Number must be positive
Step-by-Step Solution
Solution:
  1. Step 1: Analyze function behavior with input -5

    The function raises a ValueError if the number is not positive.
  2. Step 2: Understand what happens when exception is raised

    Since -5 <= 0, the function raises ValueError with the message.
  3. Final Answer:

    ValueError: Number must be positive -> Option D
  4. Quick Check:

    Exception raised = B [OK]
Quick Trick: Raise stops execution and shows error message [OK]
Common Mistakes:
MISTAKES
  • Thinking it prints the message only
  • Expecting function to return the number
  • Assuming no error is raised

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes