Bird
0
0

Why does opening a file in Ruby with mode 'a' not truncate the file, but 'w' does?

hard📝 Conceptual Q10 of 15
Ruby - File IO
Why does opening a file in Ruby with mode 'a' not truncate the file, but 'w' does?
A'a' mode truncates file; 'w' mode appends data
B'a' mode reads file; 'w' mode writes only
C'a' mode creates new file always; 'w' mode appends
D'a' mode appends data preserving existing content; 'w' mode clears file before writing
Step-by-Step Solution
Solution:
  1. Step 1: Understand 'a' mode behavior

    'a' mode opens file for appending, so it preserves existing content and adds new data at the end.
  2. Step 2: Understand 'w' mode behavior

    'w' mode opens file for writing and truncates (clears) the file before writing new content.
  3. Final Answer:

    'a' mode appends data preserving existing content; 'w' mode clears file before writing -> Option D
  4. Quick Check:

    'a' appends, 'w' truncates file = A [OK]
Quick Trick: 'a' adds, 'w' clears file before writing [OK]
Common Mistakes:
  • Confusing append with overwrite
  • Thinking 'a' truncates file
  • Mixing up read and write modes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes