Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - Error Handling
What will be the output of this Ruby code?
begin
  puts 10 / 0
rescue ZeroDivisionError
  puts "Cannot divide by zero"
end
ACannot divide by zero
B0
C10
DZeroDivisionError
Step-by-Step Solution
Solution:
  1. Step 1: Identify the error in the code

    The code tries to divide 10 by 0, which raises a ZeroDivisionError.
  2. Step 2: Understand the rescue block

    The rescue block catches ZeroDivisionError and prints "Cannot divide by zero" instead of crashing.
  3. Final Answer:

    Cannot divide by zero -> Option A
  4. Quick Check:

    ZeroDivisionError caught = message printed [OK]
Quick Trick: Divide by zero triggers rescue message [OK]
Common Mistakes:
  • Expecting program to crash
  • Thinking it prints 0 or 10
  • Confusing error name with output

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes