Bird
0
0

Which of these is the correct syntax to add an element to an array named 'arr'?

easy📝 Syntax Q3 of 15
Ruby - Arrays
Which of these is the correct syntax to add an element to an array named 'arr'?
Aarr << 5
Barr.add(5)
Carr.push 5, 6
Darr.insert(5)
Step-by-Step Solution
Solution:
  1. Step 1: Recall Ruby syntax for adding elements to arrays

    The '<<' operator appends a single element to the array.
  2. Step 2: Check each option's validity

    arr << 5 uses '<<' correctly to append a single element; arr.push 5, 6 is valid syntax but adds two elements; arr.add(5) is not a method on arrays; arr.insert(5) requires an index and value.
  3. Final Answer:

    arr << 5 -> Option A
  4. Quick Check:

    Add element syntax = arr << element [OK]
Quick Trick: Use '<<' to append elements to arrays [OK]
Common Mistakes:
  • Using add instead of <<
  • Incorrect push syntax
  • Misusing insert without index

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes