Bird
0
0

What will this Ruby code print?

medium📝 Predict Output Q5 of 15
Ruby - Functional Patterns in Ruby
What will this Ruby code print?
[1, 2, 3, 4].map(&:to_s).join(",")
A["1", "2", "3", "4"]
B"1,2,3,4"
C"1234"
D[1, 2, 3, 4]
Step-by-Step Solution
Solution:
  1. Step 1: Convert each number to string

    map(&:to_s) changes each number to its string form: ["1", "2", "3", "4"].
  2. Step 2: Join strings with commas

    join(",") combines them into one string: "1,2,3,4".
  3. Final Answer:

    "1,2,3,4" -> Option B
  4. Quick Check:

    map + join output = "1,2,3,4" [OK]
Quick Trick: map converts, join combines into string [OK]
Common Mistakes:
  • Expecting array output after join
  • Missing conversion to string
  • Confusing join separator

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes