0
0
Rubyprogramming~20 mins

Why strings are mutable in Ruby - Challenge Your Understanding

Choose your learning style9 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.