Bird
0
0

What will this code print?

medium📝 Predict Output Q5 of 15
Ruby - String Operations
What will this code print?
str = "abcdef"
puts str[-3, 2]
Acd
Bef
Cde
Derror
Step-by-Step Solution
Solution:
  1. Step 1: Understand negative start index

    str[-3, 2] starts 3 characters from the end, which is index 3 ('d').
  2. Step 2: Extract 2 characters from index 3

    Characters at index 3 and 4 are 'd' and 'e'.
  3. Final Answer:

    de -> Option C
  4. Quick Check:

    Negative index counts from end, length slices substring [OK]
Quick Trick: Negative start index counts from string end [OK]
Common Mistakes:
  • Starting at wrong index for negative value
  • Expecting 'ef' instead of 'de'
  • Thinking negative index causes error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes