Bird
0
0

You have an array of words: words = ["I", "love", "Ruby"]. Which code correctly concatenates them into a single string with spaces using ?

hard📝 Application Q8 of 15
Ruby - String Operations
You have an array of words: words = ["I", "love", "Ruby"]. Which code correctly concatenates them into a single string with spaces using <<?
Asentence = ""; words.each { |w| sentence << w << " " }; sentence.strip!
Bsentence = words.join(" ")
Csentence = ""; words.each { |w| sentence + w + " " }
Dsentence = ""; words.each { |w| sentence << w }
Step-by-Step Solution
Solution:
  1. Step 1: Understand how to append words with spaces

    sentence = ""; words.each { |w| sentence << w << " " }; sentence.strip! appends each word and a space to the string, then removes trailing space.
  2. Step 2: Compare other options

    sentence = words.join(" ") uses join (not <<), sentence = ""; words.each { |w| sentence + w + " " } does not modify sentence, sentence = ""; words.each { |w| sentence << w } misses spaces.
  3. Final Answer:

    sentence = ""; words.each { |w| sentence << w << " " }; sentence.strip! -> Option A
  4. Quick Check:

    Use << inside loop and strip trailing space [OK]
Quick Trick: Use << in loop and strip trailing space [OK]
Common Mistakes:
MISTAKES
  • Using + without assignment
  • Forgetting spaces
  • Using join instead of <<

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes