Bird
0
0

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

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

    In Ruby, 'a' mode opens a file for appending, adding data at the end.
  2. Step 2: Check other options

    'r' is read-only, 'w' is write (overwrite), 'rw' is invalid mode string.
  3. Final Answer:

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

    Append mode syntax = 'a' = C [OK]
Quick Trick: Use 'a' to add data at file end [OK]
Common Mistakes:
  • Using 'rw' which is invalid
  • Confusing 'w' with append
  • Using 'r' when writing is needed

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes