Bird
0
0

Given a file greeting.txt containing the text Hello, Ruby!, what will this code output?

medium📝 Predict Output Q4 of 15
Ruby - File IO
Given a file greeting.txt containing the text Hello, Ruby!, what will this code output?
content = File.read('greeting.txt')
puts content
AHello, Ruby!
Bcontent
CFile.read('greeting.txt')
DAn error because file is not opened
Step-by-Step Solution
Solution:
  1. Step 1: Understand File.read output

    File.read reads the entire file content as a string, so content will hold "Hello, Ruby!".
  2. Step 2: Understand puts behavior

    puts content prints the string stored in content, which is "Hello, Ruby!".
  3. Final Answer:

    Hello, Ruby! -> Option A
  4. Quick Check:

    File.read returns file text, puts prints it = B [OK]
Quick Trick: File.read returns string; puts prints that string [OK]
Common Mistakes:
  • Thinking puts prints variable name
  • Expecting error without explicit File.open
  • Confusing File.read with File.readlines

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes