Bird
0
0

Which of the following is the correct syntax to create an array of squares from 0 to 4 using Array.new with a block?

easy📝 Syntax Q3 of 15
Ruby - Arrays
Which of the following is the correct syntax to create an array of squares from 0 to 4 using Array.new with a block?
AArray.new(5) { |i| i * i }
BArray.new(5, i) { i * i }
CArray.new(5) do i * i end
DArray.new { |i| i * i }
Step-by-Step Solution
Solution:
  1. Step 1: Recall Array.new with block syntax

    The block form is Array.new(size) { |index| block } where index goes from 0 to size-1.
  2. Step 2: Check options

    Only Array.new(5) { |i| i * i } correctly uses block with index to compute squares.
  3. Final Answer:

    Array.new(5) { |i| i * i } -> Option A
  4. Quick Check:

    Array.new with block uses index for element values [OK]
Quick Trick: Use block with index for computed array elements [OK]
Common Mistakes:
  • Passing block without size
  • Wrong block syntax
  • Using second argument incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes