Bird
0
0

Which of the following is the correct syntax to get the first element of an array arr in Ruby?

easy📝 Syntax Q3 of 15
Ruby - Arrays
Which of the following is the correct syntax to get the first element of an array arr in Ruby?
Aarr.first()
Barr[0]
CAll of the above
Darr.first
Step-by-Step Solution
Solution:
  1. Step 1: Check each syntax for first element access

    arr.first() calls the method with parentheses, arr.first calls it without parentheses, arr[0] accesses by index.
  2. Step 2: Confirm all are valid in Ruby

    All three syntaxes correctly return the first element of the array.
  3. Final Answer:

    All of the above -> Option C
  4. Quick Check:

    All ways get first element [OK]
Quick Trick: First element can be accessed by arr[0] or arr.first [OK]
Common Mistakes:
MISTAKES
  • Thinking arr.first() is invalid syntax
  • Confusing arr.first with arr.last
  • Believing only arr[0] works

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes