Bird
0
0

Identify the bug in this code snippet:

medium📝 Debug Q7 of 15
Ruby - String Operations
Identify the bug in this code snippet:
str = "testing"
puts str[7]
AReturns last character 'g'
BSyntax error due to missing parentheses
CIndex 7 is out of range, returns nil
DReturns first character 't'
Step-by-Step Solution
Solution:
  1. Step 1: Check string length and index

    String 'testing' has length 7, valid indices 0 to 6.
  2. Step 2: Accessing index 7 is out of range

    str[7] returns nil because index 7 does not exist.
  3. Final Answer:

    Index 7 is out of range, returns nil -> Option C
  4. Quick Check:

    Index must be within string length [OK]
Quick Trick: Index must be less than string length [OK]
Common Mistakes:
MISTAKES
  • Expecting last character at index equal to length
  • Thinking it causes syntax error
  • Confusing zero-based indexing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes