Bird
0
0

What is wrong with this code snippet?

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

Assuming missing.txt does not exist.
AIt returns an empty array
BIt creates an empty file named missing.txt
CIt prints nil
DIt raises an exception because the file does not exist
Step-by-Step Solution
Solution:
  1. Step 1: Understand File.readlines behavior on missing file

    File.readlines raises an Errno::ENOENT exception if the file does not exist.
  2. Step 2: Confirm no silent failure

    The code will not return empty array or nil; it will stop with error.
  3. Final Answer:

    It raises an exception because the file does not exist -> Option D
  4. Quick Check:

    Missing file causes exception = B [OK]
Quick Trick: File.readlines raises error if file missing, no silent fallback [OK]
Common Mistakes:
  • Expecting empty array on missing file
  • Assuming nil is returned
  • Thinking file is created automatically

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes