Bird
Raised Fist0
Rubyprogramming~20 mins

Array creation methods in Ruby - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Ruby Array Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Ruby code creating an array?
Consider the following Ruby code that creates an array. What will be printed?
Ruby
arr = Array.new(3, 'cat')
arr[0].upcase!
puts arr.inspect
A["cat", "cat", "cat"]
B["CAT", "CAT", "CAT"]
C["CAT", "cat", "cat"]
D["cat", "CAT", "cat"]
Attempts:
2 left
💡 Hint
Think about how Array.new with a single object argument shares the same object.
❓ Predict Output
intermediate
2:00remaining
What does this Ruby array creation code output?
What will be the output of this Ruby code snippet?
Ruby
arr = Array.new(3) { 'dog' }
arr[0].upcase!
puts arr.inspect
A["DOG", "dog", "dog"]
B["DOG", "DOG", "DOG"]
C["dog", "dog", "dog"]
D["dog", "DOG", "dog"]
Attempts:
2 left
💡 Hint
Consider how the block form of Array.new creates new objects.
🧠 Conceptual
advanced
2:00remaining
Which Ruby array creation method creates independent objects?
Which of the following Ruby array creation methods ensures each element is a separate object?
AArray.new(4, [])
BArray.new(4)
CArray.new(4) { [] }
DArray.new(4, nil)
Attempts:
2 left
💡 Hint
Think about how the block form works compared to the single object argument.
❓ Predict Output
advanced
2:00remaining
What is the output of this Ruby array creation and modification?
What will this Ruby code print?
Ruby
arr = Array.new(2) { |i| i * 2 }
arr[1] = 10
puts arr.inspect
A[0, 10]
B[0, 1]
C[0, 2]
D[2, 10]
Attempts:
2 left
💡 Hint
Remember the block argument is the index of the element.
🔧 Debug
expert
2:00remaining
Why does this Ruby array creation code raise an error?
This Ruby code raises an error. What is the cause?
Ruby
arr = Array.new(3) { |i| i + 'a' }
puts arr.inspect
ANameError because 'i' is undefined
BSyntaxError due to missing parentheses
CNo error, outputs ["0a", "1a", "2a"]
DTypeError because you cannot add an integer and a string
Attempts:
2 left
💡 Hint
Check the operation inside the block carefully.

Practice

(1/5)
1. Which of the following is a correct way to create an empty array in Ruby?
easy
A. array()
B. []
C. {}
D. new Array()

Solution

  1. Step 1: Understand Ruby array syntax

    In Ruby, arrays are created using square brackets [] for literals.
  2. Step 2: Identify correct empty array syntax

    [] creates an empty array, while {} creates an empty hash, and the other options are not Ruby syntax.
  3. Final Answer:

    [] -> Option B
  4. Quick Check:

    Empty array = [] [OK]
Hint: Empty arrays use square brackets [] in Ruby [OK]
Common Mistakes:
  • Using curly braces {} which creates a hash
  • Using parentheses like array() which is invalid
  • Using JavaScript-like syntax new Array()
2. Which of the following is the correct syntax to create an array with 3 elements, all initialized to zero?
easy
A. Array.new(3, 0)
B. Array.new(0, 3)
C. Array(0, 3)
D. new Array(3, 0)

Solution

  1. Step 1: Recall Array.new syntax

    In Ruby, Array.new(size, default_value) creates an array with given size and fills it with the default value.
  2. Step 2: Check each option

    Array.new(3, 0) correctly creates [0, 0, 0]. Array.new(0, 3) creates an empty array since size is first argument. Options C and D are invalid syntax.
  3. Final Answer:

    Array.new(3, 0) -> Option A
  4. Quick Check:

    Array.new(3, 0) = [0, 0, 0] [OK]
Hint: Use Array.new(size, value) to create arrays with default values [OK]
Common Mistakes:
  • Confusing Array.new arguments order
  • Using invalid syntax like Array(0, 3)
  • Trying JavaScript style new Array()
3. What is the output of the following Ruby code?
arr = Array.new(4) { |i| i * 2 }
puts arr.inspect
medium
A. [nil, nil, nil, nil]
B. [2, 4, 6, 8]
C. [0, 2, 4, 6]
D. [0, 0, 0, 0]

Solution

  1. Step 1: Understand Array.new with block

    When Array.new is called with a block, it runs the block for each index, passing the index as i.
  2. Step 2: Calculate each element

    For indices 0 to 3, the block returns i * 2: 0*2=0, 1*2=2, 2*2=4, 3*2=6.
  3. Final Answer:

    [0, 2, 4, 6] -> Option C
  4. Quick Check:

    Array with block fills elements by index * 2 [OK]
Hint: Array.new with block uses index to set each element [OK]
Common Mistakes:
  • Assuming default value 0 for all elements
  • Confusing block argument meaning
  • Expecting nil values without block
4. The following code is intended to create an array of 3 separate empty arrays. What is the problem?
arr = Array.new(3, [])
arr[0] << 1
puts arr.inspect
medium
A. [[1], [1], [1]]
B. [[1], [], []]
C. [[], [], []]
D. SyntaxError

Solution

  1. Step 1: Understand Array.new with default object

    Using Array.new(3, []) creates 3 references to the same empty array, not 3 separate arrays.
  2. Step 2: Analyze mutation effect

    When arr[0] << 1 runs, it adds 1 to the shared array, so all elements show the change.
  3. Final Answer:

    [[1], [1], [1]] -> Option A
  4. Quick Check:

    Same object repeated causes all elements to change [OK]
Hint: Use block with Array.new for separate objects [OK]
Common Mistakes:
  • Expecting separate arrays but all share one object
  • Not using block form for unique elements
  • Assuming << affects only one element
5. How can you create an array of 5 hashes, each with keys :a and :b initialized to 0, ensuring each hash is a separate object?
hard
A. Array(5) { {a: 0, b: 0} }
B. Array.new(5, {a: 0, b: 0})
C. [{a: 0, b: 0}] * 5
D. Array.new(5) { {a: 0, b: 0} }

Solution

  1. Step 1: Understand object references in Array.new

    Using Array.new(5, {a: 0, b: 0}) or [{a: 0, b: 0}] * 5 repeats the same hash object 5 times.
  2. Step 2: Use block form for unique hashes

    Using Array.new(5) { {a: 0, b: 0} } runs the block 5 times, creating 5 separate hashes.
  3. Final Answer:

    Array.new(5) { {a: 0, b: 0} } -> Option D
  4. Quick Check:

    Block form creates unique objects [OK]
Hint: Use block with Array.new for unique hashes [OK]
Common Mistakes:
  • Using Array.new with default hash causes shared references
  • Using multiplication operator with arrays of hashes
  • Trying invalid Array() constructor syntax
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard