Bird
0
0

What will be the output of this Ruby code if the file data.txt contains the text 12345?

medium📝 Predict Output Q5 of 15
Ruby - File IO
What will be the output of this Ruby code if the file data.txt contains the text 12345?
text = File.read('data.txt')
puts text.length
A12345
B5
CAn error because length is not a method
D0
Step-by-Step Solution
Solution:
  1. Step 1: Read file content

    File.read('data.txt') returns the string "12345".
  2. Step 2: Calculate string length

    text.length returns the number of characters, which is 5.
  3. Final Answer:

    5 -> Option B
  4. Quick Check:

    String length of "12345" is 5 = B [OK]
Quick Trick: Use .length on string from File.read to get character count [OK]
Common Mistakes:
  • Expecting output to be file content instead of length
  • Thinking length method does not exist on string
  • Confusing output with file size

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes