Bird
0
0

What will this Ruby code print?

medium📝 Predict Output Q5 of 15
Ruby - Error Handling
What will this Ruby code print?
begin
  puts "Start"
ensure
  puts "Always"
end
AStart
BAlways\nStart
CStart\nAlways
DAlways
Step-by-Step Solution
Solution:
  1. Step 1: Execute the begin block

    The code prints "Start" first.
  2. Step 2: Execute the ensure block

    Then the ensure block prints "Always" regardless of errors.
  3. Final Answer:

    Start\nAlways -> Option C
  4. Quick Check:

    Ensure runs after begin, prints Always [OK]
Quick Trick: Ensure prints after begin block always [OK]
Common Mistakes:
  • Swapping print order
  • Thinking ensure runs before begin
  • Expecting only one line printed

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes