Bird
0
0

Why does str[5..3] return nil in Ruby when str = "example"?

hard📝 Conceptual Q10 of 15
Ruby - String Operations
Why does str[5..3] return nil in Ruby when str = "example"?
ABecause it returns an empty string, not nil
BBecause indices 5 and 3 are out of string bounds
CBecause Ruby does not support range slicing
DBecause the range start is greater than end, resulting in nil
Step-by-Step Solution
Solution:
  1. Step 1: Understand range slicing behavior

    In Ruby, range start must be less than or equal to end for slicing.
  2. Step 2: Check given range 5..3

    Since 5 > 3, the range is invalid and returns nil.
  3. Final Answer:

    Because the range start is greater than end, resulting in nil -> Option D
  4. Quick Check:

    Invalid range start > end returns nil [OK]
Quick Trick: Range start must be ≤ end for slicing [OK]
Common Mistakes:
  • Expecting empty string instead of nil
  • Thinking indices are out of bounds
  • Assuming Ruby supports reversed ranges

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes