Ruby - String OperationsIdentify the bug in this code snippet: str = "testing" puts str[7]AReturns last character 'g'BSyntax error due to missing parenthesesCIndex 7 is out of range, returns nilDReturns first character 't'Check Answer
Step-by-Step SolutionSolution:Step 1: Check string length and indexString 'testing' has length 7, valid indices 0 to 6.Step 2: Accessing index 7 is out of rangestr[7] returns nil because index 7 does not exist.Final Answer:Index 7 is out of range, returns nil -> Option CQuick Check:Index must be within string length [OK]Quick Trick: Index must be less than string length [OK]Common Mistakes:MISTAKESExpecting last character at index equal to lengthThinking it causes syntax errorConfusing zero-based indexing
Master "String Operations" in Ruby9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More Ruby Quizzes Arrays - Array methods (length, include?, flatten) - Quiz 5medium Hashes - Default values for missing keys - Quiz 13medium Hashes - Symbol keys vs string keys decision - Quiz 6medium Loops and Iteration - Upto and downto methods - Quiz 2easy Loops and Iteration - For loop (rarely used in Ruby) - Quiz 2easy Loops and Iteration - Each as the primary iterator - Quiz 11easy Methods - Parameters with default values - Quiz 10hard Operators and Expressions - Conditional assignment (||=) - Quiz 14medium Operators and Expressions - Comparison operators - Quiz 13medium Variables and Data Types - Integer and Float number types - Quiz 11easy