Bird
0
0

Which of the following is the correct syntax to create an array with 3 elements, all initialized to zero?

easy📝 Syntax Q12 of 15
Ruby - Arrays
Which of the following is the correct syntax to create an array with 3 elements, all initialized to zero?
A<code>Array.new(3, 0)</code>
B<code>Array.new(0, 3)</code>
C<code>Array(0, 3)</code>
D<code>new Array(3, 0)</code>
Step-by-Step Solution
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]
Quick Trick: 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()

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes