Bird
0
0

What will this Ruby code print?

medium📝 Predict Output Q5 of 15
Ruby - Error Handling
What will this Ruby code print?
def check(value)
  raise ArgumentError, "Value must be positive" if value <= 0
  "Value is #{value}"
end

puts check(-1)
ANo output
BValue is -1
Cnil
DValue must be positive (ArgumentError)
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the condition and raise

    The method raises an ArgumentError with a message if the value is less than or equal to zero.
  2. Step 2: Check the input and output

    Since -1 is less than zero, the error is raised and the program stops with that error message.
  3. Final Answer:

    Value must be positive (ArgumentError) -> Option D
  4. Quick Check:

    Raise triggers error message = A [OK]
Quick Trick: Raise stops execution if condition is true [OK]
Common Mistakes:
  • Expecting normal string output
  • Ignoring raise condition
  • Assuming method returns nil

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes