Bird
0
0

What will be the output of the following Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - Methods
What will be the output of the following Ruby code?
def greet(*names)
  names.join(", ")
end

puts greet("Alice", "Bob", "Carol")
AAlice, Bob, Carol
B["Alice", "Bob", "Carol"]
CAlice Bob Carol
DError: wrong number of arguments
Step-by-Step Solution
Solution:
  1. Step 1: Understand how *names works

    The method collects all arguments into an array called names. Here, it will be ["Alice", "Bob", "Carol"].
  2. Step 2: Analyze the join method

    names.join(", ") joins array elements into a string separated by ", ". So the output is "Alice, Bob, Carol".
  3. Final Answer:

    Alice, Bob, Carol -> Option A
  4. Quick Check:

    Array joined with commas = Alice, Bob, Carol [OK]
Quick Trick: Remember join adds separator between array elements [OK]
Common Mistakes:
  • Expecting space-separated output without commas
  • Thinking *args returns array string representation
  • Assuming error due to multiple arguments

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes