Bird
0
0

What will this Ruby code output?

medium📝 Predict Output Q4 of 15
Ruby - Error Handling
What will this Ruby code output?
begin
  result = 10 / 0
  puts result
rescue ZeroDivisionError
  puts "Division by zero is not allowed"
end
A0
BDivision by zero is not allowed
C10
DAn unhandled exception error
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the code

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

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

    Division by zero is not allowed -> Option B
  4. Quick Check:

    Does the rescue handle the division error? Yes. [OK]
Quick Trick: ZeroDivisionError triggers rescue block output [OK]
Common Mistakes:
  • Expecting the program to print 0 or 10
  • Assuming the error is unhandled
  • Ignoring the rescue block

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes