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
  file = File.open('missing.txt')
rescue Errno::ENOENT
  puts 'File not found'
end
AFile not found
Bmissing.txt
CNo output
DProgram crashes
Step-by-Step Solution
Solution:
  1. Step 1: Understand the error raised

    Trying to open a missing file raises Errno::ENOENT error.
  2. Step 2: Rescue block catches the error

    The rescue block catches this error and prints "File not found".
  3. Final Answer:

    File not found -> Option A
  4. Quick Check:

    File open error rescued = Prints message [OK]
Quick Trick: Rescue specific errors to handle file issues [OK]
Common Mistakes:
  • Expecting file name output
  • Thinking no output occurs
  • Assuming program crashes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes