Bird
0
0

What does Array.new(3, 'a') produce in Ruby?

easy📝 Conceptual Q2 of 15
Ruby - Arrays
What does Array.new(3, 'a') produce in Ruby?
AError
B['a', 'b', 'c']
C['a']
D['a', 'a', 'a']
Step-by-Step Solution
Solution:
  1. Step 1: Understand Array.new with default value

    Array.new(3, 'a') creates an array of size 3 where each element is the same object 'a'.
  2. Step 2: Confirm output

    The array will be ['a', 'a', 'a'] as all elements reference the same string.
  3. Final Answer:

    ['a', 'a', 'a'] -> Option D
  4. Quick Check:

    Array.new with default value repeats that value [OK]
Quick Trick: Array.new(size, val) repeats val in all elements [OK]
Common Mistakes:
MISTAKES
  • Expecting different elements
  • Thinking it creates copies instead of references
  • Confusing with block form

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes