Bird
Raised Fist0
Rubyprogramming~20 mins

Why strings are mutable in Ruby - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Ruby String Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Ruby code?
Consider the following Ruby code that modifies a string. What will be printed?
Ruby
str = "hello"
str.upcase!
puts str
AError: undefined method
Bhello
CHELLO\n
DHELLO
Attempts:
2 left
💡 Hint
The method upcase! changes the string itself if possible.
🧠 Conceptual
intermediate
2:00remaining
Why are Ruby strings mutable?
Why does Ruby allow strings to be changed after creation?
ABecause Ruby strings are stored as immutable objects internally
BTo improve performance by avoiding creating new objects for every change
CTo prevent accidental changes to string data
DBecause strings are always constants in Ruby
Attempts:
2 left
💡 Hint
Think about how changing strings without creating new ones affects speed and memory.
🔧 Debug
advanced
2:00remaining
What error does this Ruby code raise?
What error will this code produce and why? code: str = "hello" str.freeze str << " world"
ASyntaxError: unexpected <<
BNo error, output: "hello world"
CRuntimeError: can't modify frozen String
DTypeError: can't convert String into Integer
Attempts:
2 left
💡 Hint
Freezing an object prevents changes to it.
❓ Predict Output
advanced
2:00remaining
What is the output of this Ruby code with string mutation?
What will this Ruby code print?
Ruby
a = "cat"
b = a
b.upcase!
puts a
ACAT
Bcat
CCAT\ncat
DError: undefined method
Attempts:
2 left
💡 Hint
Variables a and b point to the same string object.
🧠 Conceptual
expert
2:00remaining
How does Ruby's string mutability affect memory usage?
Which statement best explains how Ruby's mutable strings impact memory?
AMutable strings reduce memory use by modifying existing objects instead of creating new ones
BMutable strings increase memory use because each change creates a new copy
CMutable strings have no effect on memory because Ruby uses garbage collection
DMutable strings cause memory leaks by never freeing old string data
Attempts:
2 left
💡 Hint
Think about how changing strings in place affects object creation.

Practice

(1/5)
1. Why are strings mutable in Ruby?
easy
A. Because Ruby allows changing string content without creating new objects
B. Because strings are stored as numbers internally
C. Because Ruby does not support string variables
D. Because strings are immutable in Ruby

Solution

  1. Step 1: Understand mutability concept in Ruby strings

    Ruby strings can be changed after creation, meaning their content can be updated without making a new string object.
  2. Step 2: Identify why this is useful

    This allows efficient text updates and memory use, as you don't need to create new strings for every change.
  3. Final Answer:

    Because Ruby allows changing string content without creating new objects -> Option A
  4. Quick Check:

    Ruby strings are mutable = Because Ruby allows changing string content without creating new objects [OK]
Hint: Mutability means you can change strings directly [OK]
Common Mistakes:
  • Confusing mutability with immutability
  • Thinking strings are stored as numbers
  • Believing Ruby strings cannot be changed
2. Which Ruby code correctly changes a string's content?
easy
A. str = "hello"; str.freeze
B. str = "hello"; str[0] = 'H'
C. str = "hello"; str.upcase
D. str = "hello"; str = str + '!'

Solution

  1. Step 1: Check each code for mutability

    str = "hello"; str[0] = 'H' changes the first character directly, showing string mutability.
  2. Step 2: Compare other options

    str = "hello"; str = str + '!' creates a new string; str = "hello"; str.upcase returns a new uppercase string without changing the original; str = "hello"; str.freeze freezes string making it immutable.
  3. Final Answer:

    str = "hello"; str[0] = 'H' -> Option B
  4. Quick Check:

    Direct index assignment changes string = str = "hello"; str[0] = 'H' [OK]
Hint: Use index assignment to change string characters [OK]
Common Mistakes:
  • Using freeze which prevents changes
  • Thinking concatenation changes original string
  • Confusing upcase method with direct assignment
3. What is the output of this Ruby code?
str = "cat"
str[1] = 'o'
puts str
medium
A. cat
B. c t
C. cot
D. Error

Solution

  1. Step 1: Understand string indexing and assignment

    str[1] = 'o' changes the character at index 1 (second character) from 'a' to 'o'.
  2. Step 2: Apply the change and print

    Original string "cat" becomes "cot" after the change, so puts prints "cot".
  3. Final Answer:

    cot -> Option C
  4. Quick Check:

    Changing index 1 to 'o' = cot [OK]
Hint: Index 1 means second character in string [OK]
Common Mistakes:
  • Expecting original string unchanged
  • Confusing index positions starting at 1
  • Thinking it causes an error
4. Find the error in this Ruby code that tries to change a string:
str = "dog"
str.freeze
str[0] = 'c'
puts str
medium
A. Cannot modify frozen string error
B. Syntax error on line 2
C. No error, output is "cog"
D. puts method is undefined

Solution

  1. Step 1: Understand freeze method effect

    freeze makes the string immutable, so any modification causes an error.
  2. Step 2: Identify error on modification attempt

    str[0] = 'c' tries to change the frozen string, causing a runtime error "can't modify frozen String".
  3. Final Answer:

    Cannot modify frozen string error -> Option A
  4. Quick Check:

    freeze prevents changes = error [OK]
Hint: freeze stops string changes, causing error on modification [OK]
Common Mistakes:
  • Ignoring freeze effect
  • Expecting string to change after freeze
  • Thinking freeze causes syntax error
5. You want to update multiple characters in a Ruby string efficiently. Which approach best uses string mutability?
str = "hello"
# Update 'e' and 'o' to 'a' and 'u'
hard
A. str.freeze; str[1] = 'a'
B. str = str.sub('e', 'a').sub('o', 'u')
C. str = "hallu"
D. str[1] = 'a'; str[4] = 'u'

Solution

  1. Step 1: Identify direct string mutation

    str[1] = 'a'; str[4] = 'u' changes characters by index directly, using mutability efficiently without creating new strings.
  2. Step 2: Compare other options

    str = str.sub('e', 'a').sub('o', 'u') creates new strings with sub; str = "hallu" assigns a new string; str.freeze; str[1] = 'a' freezes string then tries to change causing error.
  3. Final Answer:

    str[1] = 'a'; str[4] = 'u' -> Option D
  4. Quick Check:

    Direct index assignment updates string efficiently [OK]
Hint: Change characters by index for efficient updates [OK]
Common Mistakes:
  • Using sub which creates new strings
  • Assigning new string instead of mutating
  • Freezing string before modification
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard