Bird
0
0

What will the following Ruby code print if sample.txt contains:

medium📝 Predict Output Q5 of 15
Ruby - File IO
What will the following Ruby code print if sample.txt contains:
one\ntwo\nthree

File.readlines('sample.txt').each { |line| puts line.strip.upcase }
AONE\nTWO\nTHREE (each on separate lines)
Bone\ntwo\nthree
CONE\nTWO\nTHREE\n
DONE\nTWO\nTHREE
Step-by-Step Solution
Solution:
  1. Step 1: Analyze each line processing

    Each line is stripped of whitespace and converted to uppercase.
  2. Step 2: Understand puts behavior

    puts prints each string on its own line, so output is three lines: ONE, TWO, THREE.
  3. Final Answer:

    ONE\nTWO\nTHREE (each on separate lines) -> Option A
  4. Quick Check:

    puts prints each string on new line = D [OK]
Quick Trick: puts prints each element on a new line automatically [OK]
Common Mistakes:
  • Thinking output is a single line string
  • Ignoring strip removing newline
  • Confusing print and puts behavior

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes