Bird
0
0

Which of the following correctly accesses the first element of an array arr in Ruby using indexing?

easy📝 Conceptual Q11 of 15
Ruby - Hashes
Which of the following correctly accesses the first element of an array arr in Ruby using indexing?
A<code>arr[1]</code>
B<code>arr.first()</code>
C<code>arr[0]</code>
D<code>arr.get(0)</code>
Step-by-Step Solution
Solution:
  1. Step 1: Understand array indexing in Ruby

    Ruby arrays start at index 0, so the first element is at arr[0].
  2. Step 2: Check each option

    arr[0] correctly accesses the first element. arr[1] accesses the second element. arr.first() is a method call that also returns the first element but is not using square brackets. arr.get(0) is invalid syntax in Ruby.
  3. Final Answer:

    arr[0] -> Option C
  4. Quick Check:

    First element index = 0 [OK]
Quick Trick: Remember: array indexes start at zero in Ruby [OK]
Common Mistakes:
  • Using index 1 for first element
  • Using method calls instead of brackets
  • Trying to use non-existent methods like get()

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes