Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q5 of 15
Ruby - Arrays
What will be the output of this Ruby code?
arr = [100, 200, 300]
puts arr.first(2).join('-')
A100
B100-200
C200-300
DError
Step-by-Step Solution
Solution:
  1. Step 1: Understand arr.first(2)

    arr.first(2) returns an array of the first two elements: [100, 200].
  2. Step 2: Join the elements with '-'

    Joining [100, 200] with '-' produces the string '100-200'.
  3. Final Answer:

    100-200 -> Option B
  4. Quick Check:

    arr.first(2).join('-') = '100-200' [OK]
Quick Trick: arr.first(n) returns first n elements as array [OK]
Common Mistakes:
  • Thinking arr.first(2) returns single element
  • Expecting join to cause error
  • Confusing arr.first with arr.last

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes