Challenge - 5 Problems
Ruby String Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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
Attempts:
2 left
💡 Hint
The method upcase! changes the string itself if possible.
✗ Incorrect
In Ruby, strings are mutable, so methods ending with ! modify the original string. upcase! changes all letters to uppercase in place.
🧠 Conceptual
intermediate2:00remaining
Why are Ruby strings mutable?
Why does Ruby allow strings to be changed after creation?
Attempts:
2 left
💡 Hint
Think about how changing strings without creating new ones affects speed and memory.
✗ Incorrect
Ruby strings are mutable to allow efficient modifications without creating new string objects each time, improving performance.
🔧 Debug
advanced2:00remaining
What error does this Ruby code raise?
What error will this code produce and why?
code:
str = "hello"
str.freeze
str << " world"
Attempts:
2 left
💡 Hint
Freezing an object prevents changes to it.
✗ Incorrect
Calling freeze on a string makes it immutable. Trying to append with << raises a RuntimeError.
❓ Predict Output
advanced2: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
Attempts:
2 left
💡 Hint
Variables a and b point to the same string object.
✗ Incorrect
Both a and b refer to the same string. upcase! changes the string in place, so a also shows the change.
🧠 Conceptual
expert2:00remaining
How does Ruby's string mutability affect memory usage?
Which statement best explains how Ruby's mutable strings impact memory?
Attempts:
2 left
💡 Hint
Think about how changing strings in place affects object creation.
✗ Incorrect
Because Ruby strings can be changed in place, fewer new objects are created, saving memory.