Bird
0
0

The following Ruby code is intended to append "More text" to the file "notes.txt". What is the error?

medium📝 Debug Q14 of 15
Ruby - File IO
The following Ruby code is intended to append "More text" to the file "notes.txt". What is the error?
File.write("notes.txt", "More text")
AThe filename should be a symbol, not a string.
BFile.write overwrites instead of appending; mode 'a' is missing.
CThe string "More text" must be inside single quotes.
DFile.write cannot write strings, only bytes.
Step-by-Step Solution
Solution:
  1. Step 1: Understand File.write default mode

    By default, File.write overwrites the file content.
  2. Step 2: Identify how to append

    To append, you must specify mode: File.write("notes.txt", "More text", mode: "a").
  3. Final Answer:

    File.write overwrites instead of appending; mode 'a' is missing. -> Option B
  4. Quick Check:

    Append needs mode 'a' = A [OK]
Quick Trick: Use mode: 'a' to append with File.write [OK]
Common Mistakes:
  • Not specifying mode to append
  • Using symbols instead of strings for filename
  • Thinking quotes style affects writing
  • Believing File.write can't write strings

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes