Bird
0
0

What is the output of the following Ruby code?

medium📝 Predict Output Q4 of 15
Ruby - Arrays
What is the output of the following Ruby code?
arr = [10, 20, 30, 40, 50]
slice = arr[1..3]
puts slice.inspect
A[20, 30, 40]
B[20, 30]
C[10, 20, 30]
D[30, 40, 50]
Step-by-Step Solution
Solution:
  1. Step 1: Understand the range 1..3

    The range includes indices 1, 2, and 3.
  2. Step 2: Extract elements at indices 1, 2, and 3

    These elements are 20, 30, and 40 respectively.
  3. Final Answer:

    [20, 30, 40] -> Option A
  4. Quick Check:

    Range 1..3 extracts elements at indices 1 to 3 inclusive = [20, 30, 40] [OK]
Quick Trick: .. includes end index in slicing [OK]
Common Mistakes:
MISTAKES
  • Using ... which excludes the last element
  • Off-by-one errors in indices
  • Confusing array indices with values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes