Bird
0
0

What is wrong with this Ruby code snippet?

medium📝 Debug Q14 of 15
Ruby - File IO
What is wrong with this Ruby code snippet?
content = File.read('missing.txt')
puts content

Assume the file missing.txt does not exist.
AIt will raise an error because the file does not exist.
BIt will print an empty string.
CIt will create the file automatically.
DIt will print nil.
Step-by-Step Solution
Solution:
  1. Step 1: Check File.read behavior on missing file

    If the file does not exist, File.read raises an exception (Errno::ENOENT).
  2. Step 2: Understand program flow

    Since the exception is not handled, the program stops and does not print anything.
  3. Final Answer:

    It will raise an error because the file does not exist. -> Option A
  4. Quick Check:

    Missing file causes error on File.read [OK]
Quick Trick: File.read errors if file missing, handle exceptions [OK]
Common Mistakes:
  • Assuming it returns empty string
  • Thinking it creates missing file
  • Expecting nil instead of error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes