Bird
0
0

What does the Ruby method File.write do by default when writing to a file?

easy📝 Conceptual Q11 of 15
Ruby - File IO
What does the Ruby method File.write do by default when writing to a file?
AIt creates the file if it doesn't exist and overwrites existing content.
BIt appends new content to the end of the file without changing existing content.
CIt only reads the file content without modifying it.
DIt deletes the file before writing new content.
Step-by-Step Solution
Solution:
  1. Step 1: Understand File.write default behavior

    By default, File.write creates the file if it does not exist.
  2. Step 2: Check how it handles existing files

    If the file exists, it overwrites the entire content with the new data.
  3. Final Answer:

    It creates the file if it doesn't exist and overwrites existing content. -> Option A
  4. Quick Check:

    File.write overwrites by default = A [OK]
Quick Trick: Remember: File.write overwrites unless mode is changed [OK]
Common Mistakes:
  • Thinking File.write appends by default
  • Assuming File.write only reads files
  • Believing File.write deletes files before writing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes