Challenge - 5 Problems
Frozen Object 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 with frozen string?
Consider this Ruby code snippet:
What will be the output or error?
str = "hello".freeze str << " world" puts str
What will be the output or error?
Ruby
str = "hello".freeze str << " world" puts str
Attempts:
2 left
💡 Hint
Frozen objects cannot be changed after freezing.
✗ Incorrect
The string is frozen, so trying to modify it with << causes a RuntimeError.
❓ Predict Output
intermediate2:00remaining
What happens when modifying a frozen array element?
Look at this Ruby code:
What will be the output or error?
arr = [1, 2, 3].freeze arr[0] = 10 puts arr.inspect
What will be the output or error?
Ruby
arr = [1, 2, 3].freeze arr[0] = 10 puts arr.inspect
Attempts:
2 left
💡 Hint
Freezing an array prevents changing its elements.
✗ Incorrect
The array is frozen, so assigning to an element raises RuntimeError.
❓ Predict Output
advanced2:00remaining
What is the output when freezing a hash and modifying a nested object?
Consider this Ruby code:
What will be printed or what error occurs?
h = {a: [1, 2]}
h.freeze
h[:a] << 3
puts h[:a].inspectWhat will be printed or what error occurs?
Ruby
h = {a: [1, 2]}
h.freeze
h[:a] << 3
puts h[:a].inspectAttempts:
2 left
💡 Hint
Freezing a hash does not freeze its nested objects.
✗ Incorrect
The hash is frozen, but its array value is not frozen, so << works and adds 3.
❓ Predict Output
advanced2:00remaining
What error occurs when freezing a string literal and modifying it?
Analyze this Ruby code:
What will happen when running this code?
def modify_str(s) s << "!" end str = "hello".freeze modify_str(str) puts str
What will happen when running this code?
Ruby
def modify_str(s) s << "!" end str = "hello".freeze modify_str(str) puts str
Attempts:
2 left
💡 Hint
Frozen strings cannot be changed even inside methods.
✗ Incorrect
The frozen string cannot be modified by << inside the method, causing RuntimeError.
🧠 Conceptual
expert3:00remaining
Why does freezing an object not freeze its nested objects in Ruby?
In Ruby, when you freeze an object like a hash or array, its nested objects remain mutable.
Why does Ruby behave this way?
Why does Ruby behave this way?
Attempts:
2 left
💡 Hint
Think about how objects and references work in Ruby.
✗ Incorrect
Freezing marks only the object itself as immutable; nested objects are separate and need their own freeze calls.