Bird
0
0

Which method or syntax in Ruby returns the first element of an array arr?

easy📝 Conceptual Q11 of 15
Ruby - Arrays
Which method or syntax in Ruby returns the first element of an array arr?
A<code>arr.last</code>
B<code>arr[1]</code>
C<code>arr[-1]</code>
D<code>arr.first</code>
Step-by-Step Solution
Solution:
  1. Step 1: Understand the purpose of .first

    The .first method returns the first element of an array in Ruby.
  2. Step 2: Compare with other options

    arr[1] returns the second element (indexing starts at 0), arr.last and arr[-1] return the last element.
  3. Final Answer:

    arr.first -> Option D
  4. Quick Check:

    First element = arr.first [OK]
Quick Trick: Use .first to get the first element easily [OK]
Common Mistakes:
  • Using arr[1] instead of arr[0] for first element
  • Confusing .last with .first
  • Using negative index for first element

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes