Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q4 of 15
Ruby - Error Handling
What will be the output of this Ruby code?
begin
  puts 100 / 0
rescue ZeroDivisionError
  puts "Division by zero error caught"
end
A0
BDivision by zero error caught
CInfinity
DRuntimeError
Step-by-Step Solution
Solution:
  1. Step 1: Understand the code

    The code attempts to divide 100 by 0, which raises a ZeroDivisionError.
  2. Step 2: Rescue block handling

    The rescue ZeroDivisionError block catches this exception and prints the message.
  3. Final Answer:

    Division by zero error caught -> Option B
  4. Quick Check:

    ZeroDivisionError triggers rescue block output [OK]
Quick Trick: Division by zero triggers rescue block output [OK]
Common Mistakes:
  • Assuming division by zero returns 0 or Infinity
  • Not rescuing specific exception type

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes