Bird
0
0

What is the output of this Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - Error Handling
What is the output of this Ruby code?
value = Integer('abc') rescue 'error'
puts value
Aabc
Berror
C0
DRaises an exception
Step-by-Step Solution
Solution:
  1. Step 1: Understand Integer conversion with invalid string

    Calling Integer('abc') raises an ArgumentError because 'abc' is not a valid number.
  2. Step 2: Apply rescue modifier

    The rescue modifier catches this error and returns the string 'error' instead of crashing.
  3. Final Answer:

    error -> Option B
  4. Quick Check:

    Invalid Integer conversion rescued to 'error' [OK]
Quick Trick: Rescue returns fallback when conversion fails [OK]
Common Mistakes:
  • Expecting the original string as output
  • Thinking rescue returns nil by default
  • Assuming code raises exception without rescue

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes