Bird
Raised Fist0
Rubyprogramming~20 mins

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

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

Practice

(1/5)
1. What does the freeze method do to an object in Ruby?
easy
A. Prevents any further modifications to the object
B. Deletes the object from memory
C. Duplicates the object
D. Makes the object mutable

Solution

  1. Step 1: Understand the purpose of freeze

    The freeze method is used to make an object immutable, meaning it cannot be changed after freezing.
  2. Step 2: Identify the effect on the object

    Once frozen, any attempt to modify the object will raise an error, so it prevents further changes.
  3. Final Answer:

    Prevents any further modifications to the object -> Option A
  4. Quick Check:

    freeze makes object immutable = Prevents changes [OK]
Hint: Freeze means no changes allowed after calling it [OK]
Common Mistakes:
  • Thinking freeze deletes the object
  • Confusing freeze with duplication
  • Assuming freeze makes object mutable
2. Which of the following is the correct way to freeze a string name in Ruby?
easy
A. freeze(name)
B. name.frozen = true
C. name.freeze()
D. freeze name

Solution

  1. Step 1: Recall Ruby method call syntax

    In Ruby, to call a method on an object, use dot notation like object.method().
  2. Step 2: Identify the correct freeze syntax

    The correct way to freeze the string name is name.freeze(). Other options are invalid syntax or incorrect usage.
  3. Final Answer:

    name.freeze() -> Option C
  4. Quick Check:

    Method call with dot and parentheses = Correct freeze syntax [OK]
Hint: Use dot notation: object.freeze() to freeze [OK]
Common Mistakes:
  • Using freeze as a standalone function
  • Trying to assign frozen = true
  • Omitting dot before freeze
3. What will be the output of this Ruby code?
arr = [1, 2, 3]
arr.freeze
arr << 4
puts arr.inspect
medium
A. [1, 2, 3, 4]
B. RuntimeError: can't modify frozen Array
C. [1, 2, 3]
D. SyntaxError

Solution

  1. Step 1: Understand freezing the array

    The array arr is frozen using arr.freeze, making it immutable.
  2. Step 2: Analyze the modification attempt

    Trying to append 4 with arr << 4 will raise a RuntimeError because the array is frozen and cannot be changed.
  3. Final Answer:

    RuntimeError: can't modify frozen Array -> Option B
  4. Quick Check:

    Frozen object + modification = RuntimeError [OK]
Hint: Modifying frozen object causes RuntimeError [OK]
Common Mistakes:
  • Assuming frozen array can still be changed
  • Expecting silent failure or no error
  • Confusing output with syntax error
4. Identify the error in this Ruby code snippet:
str = "hello"
str.freeze
str[0] = 'H'
puts str
medium
A. RuntimeError: can't modify frozen String
B. SyntaxError due to invalid string modification
C. No error, output is "Hello"
D. Output is "hello" without changes

Solution

  1. Step 1: Recognize string freezing effect

    The string str is frozen, so it cannot be changed after str.freeze.
  2. Step 2: Understand the attempted modification

    Trying to change the first character with str[0] = 'H' raises a RuntimeError because the string is frozen.
  3. Final Answer:

    RuntimeError: can't modify frozen String -> Option A
  4. Quick Check:

    Frozen string + modification = RuntimeError [OK]
Hint: Frozen string cannot be changed, causes RuntimeError [OK]
Common Mistakes:
  • Thinking string changes silently fail
  • Expecting output "Hello" without error
  • Confusing syntax error with runtime error
5. Given this code, what is the best way to safely modify a frozen array without error?
original = [10, 20, 30].freeze
# Your code here to add 40
hard
A. original.freeze(false)
B. original << 40
C. original.push(40)
D. original = original.dup << 40

Solution

  1. Step 1: Understand frozen array cannot be changed

    The array original is frozen, so direct modification like << or push will cause errors.
  2. Step 2: Use duplication to create a modifiable copy

    Using original.dup creates a new unfrozen copy. Then appending 40 to this copy is allowed.
  3. Step 3: Assign the modified copy back

    Assigning the new array with 40 added back to original variable safely updates it without error.
  4. Final Answer:

    original = original.dup << 40 -> Option D
  5. Quick Check:

    Duplicate frozen object before modifying = Safe change [OK]
Hint: Duplicate frozen object before changing it [OK]
Common Mistakes:
  • Trying to modify frozen array directly
  • Using non-existent freeze(false) method
  • Assuming push works on frozen array
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