Bird
0
0

What is the output of this Ruby code?

medium📝 Predict Output Q4 of 15
Ruby - String Operations
What is the output of this Ruby code?
words = "red green blue".split(' ')
result = words.join('-')
puts result
Ared,green,blue
Bred green blue
Credgreenblue
Dred-green-blue
Step-by-Step Solution
Solution:
  1. Step 1: Split the string by space

    "red green blue".split(' ') creates the array ["red", "green", "blue"].
  2. Step 2: Join the array with '-'

    Joining with '-' results in the string "red-green-blue".
  3. Final Answer:

    red-green-blue -> Option D
  4. Quick Check:

    split + join with '-' = red-green-blue [OK]
Quick Trick: Split by space, join with '-' creates hyphenated string [OK]
Common Mistakes:
MISTAKES
  • Forgetting join separator
  • Using split without argument
  • Expecting commas instead of hyphens

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes