0
0
Rubyprogramming~20 mins

Immutable data with freeze in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ruby Freeze Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of freezing a string and then modifying it?
Consider the following Ruby code. What will be the output when it is run?
Ruby
str = "hello"
str.freeze
begin
  str << " world"
rescue => e
  puts e.class
end
Ahello world
BTypeError
CNo output
DRuntimeError
Attempts:
2 left
💡 Hint
Freezing an object prevents modifications. What happens if you try to change a frozen string?
Predict Output
intermediate
2:00remaining
What happens when you freeze an array and try to modify it?
Look at this Ruby code. What will be printed when it runs?
Ruby
arr = [1, 2, 3]
arr.freeze
begin
  arr.push(4)
rescue => e
  puts e.class
end
ARuntimeError
BTypeError
C[1, 2, 3, 4]
DNo output
Attempts:
2 left
💡 Hint
Freezing an array stops changes like adding elements. What error is raised if you try?
🧠 Conceptual
advanced
2:00remaining
Which statement about freezing nested objects is true?
Given a frozen array that contains other arrays, which statement is correct?
AFreezing the outer array converts nested arrays into frozen arrays automatically.
BFreezing the outer array also freezes all nested arrays inside it.
CNested arrays remain mutable unless they are individually frozen.
DFreezing the outer array prevents adding new nested arrays but allows modifying existing nested arrays.
Attempts:
2 left
💡 Hint
Think about whether freeze applies recursively or only to the object itself.
Predict Output
advanced
2:00remaining
What is the output when freezing a hash and modifying a nested array value?
Analyze this Ruby code and determine the output:
Ruby
h = {a: [1, 2], b: [3, 4]}
h.freeze
begin
  h[:a] << 5
rescue => e
  puts e.class
end
puts h[:a].inspect
A
RuntimeError
[1, 2]
B[1, 2, 5]
C
RuntimeError
[1, 2, 5]
D
TypeError
[1, 2]
Attempts:
2 left
💡 Hint
Freezing a hash stops changing keys or values, but what about modifying the objects inside the hash?
🔧 Debug
expert
3:00remaining
Why does this code raise an error when freezing a nested structure?
This Ruby code tries to freeze a nested array structure. Why does it raise an error?
Ruby
def deep_freeze(obj)
  obj.freeze
  if obj.is_a?(Array)
    obj.each { |e| deep_freeze(e) }
  elsif obj.is_a?(Hash)
    obj.each { |k, v| deep_freeze(k); deep_freeze(v) }
  end
end

nested = [1, [2, 3], {a: 4}]
deep_freeze(nested)
nested[1] << 5
AIt raises a RuntimeError because nested[1] is frozen and cannot be modified.
BIt raises a TypeError because deep_freeze does not handle arrays correctly.
CIt raises a NoMethodError because freeze is not defined for integers.
DIt does not raise an error; the code runs successfully.
Attempts:
2 left
💡 Hint
Check what deep_freeze does to nested arrays and what happens when you try to modify a frozen array.