Bird
0
0

What is the output of this Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - String Operations
What is the output of this Ruby code?
str = "programming"
puts str[-4, 3]
Aing
Bram
Camm
Dmin
Step-by-Step Solution
Solution:
  1. Step 1: Understand negative indexing and slicing

    Negative index -4 means counting from the end: 'g'( -1), 'n'( -2), 'i'( -3), 'm'( -4). So index -4 points to 'm'.
  2. Step 2: Extract substring starting at 'm' with length 3

    From 'm' take 3 characters: 'm', 'i', 'n' forming 'min'.
  3. Final Answer:

    min -> Option D
  4. Quick Check:

    str[-4,3] = 'min' [OK]
Quick Trick: Negative index counts from end; length slices forward [OK]
Common Mistakes:
  • Confusing negative index position
  • Using range instead of start,length
  • Off-by-one errors in slicing length

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes