Bird
0
0

What is the output of the following Ruby code?

medium📝 Predict Output Q4 of 15
Ruby - Hashes
What is the output of the following Ruby code?
arr = [5, 10, 15]
arr[0] = 20
puts arr.inspect
A[10, 20, 15]
B[5, 10, 15]
C[20, 15, 10]
D[20, 10, 15]
Step-by-Step Solution
Solution:
  1. Step 1: Understand array element assignment

    Assigning arr[0] = 20 changes the first element from 5 to 20.
  2. Step 2: Check array content after assignment

    The array becomes [20, 10, 15].
  3. Final Answer:

    [20, 10, 15] -> Option D
  4. Quick Check:

    Array after assignment = [20, 10, 15] [OK]
Quick Trick: Index 0 is first element; assignment changes its value [OK]
Common Mistakes:
  • Assuming original array unchanged
  • Mixing element order
  • Confusing index positions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes