Bird
Raised Fist0
Rubyprogramming~20 mins

Accessing elements (indexing, first, last) in Ruby - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Ruby Array Access Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of accessing the first and last elements?
Consider the Ruby array arr = [10, 20, 30, 40, 50]. What will be printed by the following code?
puts arr.first
puts arr.last
Ruby
arr = [10, 20, 30, 40, 50]
puts arr.first
puts arr.last
A10\n10
B50\n10
C10\n50
D50\n50
Attempts:
2 left
💡 Hint
Remember, first gives the first element and last gives the last element of an array.
❓ Predict Output
intermediate
2:00remaining
What is the output of negative indexing in Ruby arrays?
Given the array nums = [5, 10, 15, 20, 25], what will puts nums[-2] print?
Ruby
nums = [5, 10, 15, 20, 25]
puts nums[-2]
A15
B20
C25
D10
Attempts:
2 left
💡 Hint
Negative indices count from the end of the array, starting at -1 for the last element.
❓ Predict Output
advanced
2:00remaining
What happens when accessing an out-of-range index?
What will the following Ruby code print?
arr = [1, 2, 3]
puts arr[5].inspect
Ruby
arr = [1, 2, 3]
puts arr[5].inspect
Anil
BIndexError
C3
D0
Attempts:
2 left
💡 Hint
Ruby returns nil when you access an index outside the array bounds.
❓ Predict Output
advanced
2:00remaining
What is the output when using first(n) and last(n) methods?
Given arr = [7, 14, 21, 28, 35], what will the following code print?
p arr.first(3)
p arr.last(2)
Ruby
arr = [7, 14, 21, 28, 35]
p arr.first(3)
p arr.last(2)
A[7, 14]\n[28, 35]
B[7, 14, 21]\n[21, 28]
C[14, 21, 28]\n[28, 35]
D[7, 14, 21]\n[28, 35]
Attempts:
2 left
💡 Hint
The first(n) method returns the first n elements, and last(n) returns the last n elements.
🧠 Conceptual
expert
3:00remaining
What is the value of result after this code runs?
Consider this Ruby code:
arr = ['a', 'b', 'c', 'd']
result = arr[-1] + arr[0] + arr[-3]

What is the value of result?
Ruby
arr = ['a', 'b', 'c', 'd']
result = arr[-1] + arr[0] + arr[-3]
A"dab"
B"d a b"
C"dcb"
D"dac"
Attempts:
2 left
💡 Hint
Remember that negative indices count from the end: -1 is last, -2 is second last, etc.

Practice

(1/5)
1. Which method or syntax in Ruby returns the first element of an array arr?
easy
A. arr.last
B. arr[1]
C. arr[-1]
D. arr.first

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]
Hint: 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
2. Which of the following is the correct syntax to get the last element of an array arr in Ruby?
easy
A. arr.first
B. arr[0]
C. arr[-1]
D. arr[1]

Solution

  1. Step 1: Understand negative indexing in Ruby arrays

    Negative indexes count from the end, so arr[-1] returns the last element.
  2. Step 2: Check other options

    arr[0] is the first element, arr.first also returns first, and arr[1] is second element.
  3. Final Answer:

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

    Last element = arr[-1] [OK]
Hint: Use negative index -1 to get last element [OK]
Common Mistakes:
  • Using arr[1] thinking it's last element
  • Using arr.first instead of arr.last
  • Using arr[-0] which is invalid
3. What is the output of the following Ruby code?
arr = [10, 20, 30, 40, 50]
puts arr[2]
puts arr.first
puts arr[-2]
medium
A. 40 10 30
B. 30 10 40
C. 30 50 40
D. 20 10 50

Solution

  1. Step 1: Evaluate arr[2]

    Index 2 is the third element, which is 30.
  2. Step 2: Evaluate arr.first and arr[-2]

    arr.first is 10 (first element), arr[-2] is second last element, 40.
  3. Final Answer:

    30 10 40 -> Option B
  4. Quick Check:

    Index 2 = 30, first = 10, second last = 40 [OK]
Hint: Remember index starts at 0; negative counts from end [OK]
Common Mistakes:
  • Confusing index 2 with index 3
  • Mixing up first and last elements
  • Misunderstanding negative indexes
4. The following Ruby code throws an error. What is the problem?
arr = [5, 10, 15]
puts arr.first()
puts arr[-4]
medium
A. Negative index -4 is out of range and returns nil, no error
B. Missing argument in first() causes error
C. Array is empty, so first() fails
D. Syntax error in calling first()

Solution

  1. Step 1: Check arr.first() usage

    first() can be called with or without parentheses; no error here.
  2. Step 2: Analyze arr[-4]

    Array has 3 elements, so index -4 is out of range and returns nil, which prints as empty line, no error.
  3. Final Answer:

    Negative index -4 is out of range and returns nil, no error -> Option A
  4. Quick Check:

    Out of range negative index returns nil [OK]
Hint: Out of range negative index returns nil, no error [OK]
Common Mistakes:
  • Assuming out of range index causes error
  • Thinking first() needs arguments
  • Confusing nil with error
5. Given the array arr = ["a", "b", "c", "d", "e"], which Ruby expression returns a new array with only the first and last elements?
hard
A. [arr.first, arr.last]
B. arr[0, 1] + arr[-1]
C. arr[0] + arr[-1]
D. arr.first + arr.last

Solution

  1. Step 1: Understand the goal

    We want a new array containing only the first and last elements.
  2. Step 2: Evaluate each option

    [arr.first, arr.last] creates an array with arr.first and arr.last. arr[0, 1] + arr[-1] tries to add an array and a string (wrong). arr[0] + arr[-1] adds two strings, resulting in concatenation, not array. arr.first + arr.last adds two strings, also concatenation.
  3. Final Answer:

    [arr.first, arr.last] -> Option A
  4. Quick Check:

    Array with first and last elements = [arr.first, arr.last] [OK]
Hint: Use array brackets to combine first and last elements [OK]
Common Mistakes:
  • Adding strings instead of creating an array
  • Using + with array and string causing error
  • Forgetting to wrap elements in array brackets
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard