Bird
0
0

Which of the following is the correct syntax to open a file named data.txt for appending in Ruby?

easy📝 Syntax Q12 of 15
Ruby - File IO
Which of the following is the correct syntax to open a file named data.txt for appending in Ruby?
AFile.open('data.txt', 'x')
BFile.open('data.txt', 'w')
CFile.open('data.txt', 'r')
DFile.open('data.txt', 'a')
Step-by-Step Solution
Solution:
  1. Step 1: Identify the mode for appending

    The mode 'a' is used to open a file for appending new content at the end.
  2. Step 2: Check the syntax

    The correct syntax is File.open('filename', 'a') to append.
  3. Final Answer:

    File.open('data.txt', 'a') -> Option D
  4. Quick Check:

    Append mode = 'a' [OK]
Quick Trick: Use 'a' to add content without erasing [OK]
Common Mistakes:
  • Using 'r' which is read-only mode
  • Using 'w' which erases file content
  • Using 'x' which creates a new file and errors if exists

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes