Bird
0
0

Given a string data = "red;green;blue;yellow", write Ruby code to split it into an array, remove the color "blue", and then join the remaining colors with commas. What is the correct output?

hard📝 Application Q15 of 15
Ruby - String Operations
Given a string data = "red;green;blue;yellow", write Ruby code to split it into an array, remove the color "blue", and then join the remaining colors with commas. What is the correct output?
A"red;green;yellow"
B"red,green,blue,yellow"
C"red green yellow"
D"red,green,yellow"
Step-by-Step Solution
Solution:
  1. Step 1: Split the string by semicolons

    Using split(";") on data gives ["red", "green", "blue", "yellow"].
  2. Step 2: Remove "blue" from the array

    Remove "blue" by using array.delete("blue") or array.reject!.
  3. Step 3: Join the remaining elements with commas

    Joining with join(",") results in "red,green,yellow".
  4. Final Answer:

    "red,green,yellow" -> Option D
  5. Quick Check:

    Split by ;, remove blue, join by , = red,green,yellow [OK]
Quick Trick: Split by ; remove item, join by , for final string [OK]
Common Mistakes:
  • Forgetting to remove "blue" before joining
  • Joining with wrong separator
  • Using split without argument or wrong separator

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes