Bird
0
0

What is the output of this Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - String Operations
What is the output of this Ruby code?
sentence = "apple,banana,cherry"
fruits = sentence.split(",")
result = fruits.join(" & ")
puts result
Aapple & banana & cherry
Bapple,banana,cherry
Capple banana cherry
D["apple", "banana", "cherry"]
Step-by-Step Solution
Solution:
  1. Step 1: Split the string by commas

    The split(",") breaks the string into ["apple", "banana", "cherry"].
  2. Step 2: Join the array with " & " separator

    The join(" & ") combines the array elements into the string "apple & banana & cherry".
  3. Final Answer:

    apple & banana & cherry -> Option A
  4. Quick Check:

    split then join with & = apple & banana & cherry [OK]
Quick Trick: Split by comma, join with & gives 'apple & banana & cherry' [OK]
Common Mistakes:
  • Expecting original string without changes
  • Confusing split and join order
  • Thinking join adds commas automatically

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes