Bird
0
0

Which method creates an array with 5 nil elements in Ruby?

easy📝 Conceptual Q1 of 15
Ruby - Arrays
Which method creates an array with 5 nil elements in Ruby?
AArray.new(5)
BArray[5]
C[5].new
DArray.create(5)
Step-by-Step Solution
Solution:
  1. Step 1: Understand Array.new behavior

    Array.new(5) creates an array with 5 elements, all set to nil by default.
  2. Step 2: Check other options

    Array[5] creates an array with one element 5, [5].new and Array.create(5) are invalid syntax.
  3. Final Answer:

    Array.new(5) -> Option A
  4. Quick Check:

    Array creation with nil elements = Array.new(5) [OK]
Quick Trick: Use Array.new(size) for nil-filled arrays [OK]
Common Mistakes:
  • Using Array[5] creates single element array
  • Trying invalid methods like Array.create
  • Confusing array literal with constructor

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes