Bird
Raised Fist0
Rubyprogramming~20 mins

Frozen objects in Ruby - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Frozen Object 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 with frozen string?
Consider this Ruby code snippet:
str = "hello".freeze
str << " world"
puts str

What will be the output or error?
Ruby
str = "hello".freeze
str << " world"
puts str
ARuntimeError: can't modify frozen String
Bhello world
CTypeError: no implicit conversion of String into Integer
Dhello
Attempts:
2 left
💡 Hint
Frozen objects cannot be changed after freezing.
❓ Predict Output
intermediate
2:00remaining
What happens when modifying a frozen array element?
Look at this Ruby code:
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
ARuntimeError: can't modify frozen Array
B[10, 2, 3]
C[1, 2, 3]
DTypeError: no implicit conversion of Integer into String
Attempts:
2 left
💡 Hint
Freezing an array prevents changing its elements.
❓ Predict Output
advanced
2:00remaining
What is the output when freezing a hash and modifying a nested object?
Consider this Ruby code:
h = {a: [1, 2]}
h.freeze
h[:a] << 3
puts h[:a].inspect

What will be printed or what error occurs?
Ruby
h = {a: [1, 2]}
h.freeze
h[:a] << 3
puts h[:a].inspect
A[1, 2]
B[1, 2, 3]
CRuntimeError: can't modify frozen Hash
DTypeError: no implicit conversion of Integer into String
Attempts:
2 left
💡 Hint
Freezing a hash does not freeze its nested objects.
❓ Predict Output
advanced
2:00remaining
What error occurs when freezing a string literal and modifying it?
Analyze this Ruby 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
Ahello!
BNo output, program hangs
CRuntimeError: can't modify frozen String
DTypeError: no implicit conversion of String into Integer
Attempts:
2 left
💡 Hint
Frozen strings cannot be changed even inside methods.
🧠 Conceptual
expert
3: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?
ABecause freezing an object disables garbage collection for nested objects.
BBecause Ruby automatically freezes nested objects only if they are strings.
CBecause freezing an object clones all nested objects and freezes the clones instead.
DBecause freeze only marks the top-level object as immutable; nested objects are separate and must be frozen individually.
Attempts:
2 left
💡 Hint
Think about how objects and references work in Ruby.

Practice

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

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 modifications.
  3. Final Answer:

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

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

Solution

  1. Step 1: Recall Ruby method call syntax

    In Ruby, methods are called on objects using dot notation, e.g., object.method.
  2. Step 2: Identify the correct freeze syntax

    The correct way to freeze an object is by calling freeze on it: str.freeze(). The other options are invalid syntax.
  3. Final Answer:

    str.freeze() -> Option A
  4. Quick Check:

    Method call syntax = str.freeze() [OK]
Hint: Use dot notation to call freeze: object.freeze() [OK]
Common Mistakes:
  • Using freeze as a standalone keyword
  • Assigning freeze as a property
  • Calling freeze like a function without dot
3. What will be the output of this Ruby code?
arr = [1, 2, 3]
arr.freeze
arr << 4
puts arr.inspect
medium
A. RuntimeError: can't modify frozen Array
B. [1, 2, 3, 4]
C. [1, 2, 3]
D. SyntaxError

Solution

  1. Step 1: Understand freezing the array

    The array arr is frozen with arr.freeze, so it cannot be changed.
  2. Step 2: Analyze the modification attempt

    The code tries to add an element with arr << 4, which modifies the array. Since it's frozen, this raises a runtime error.
  3. Final Answer:

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

    Modifying frozen object = RuntimeError [OK]
Hint: Modifying frozen objects causes runtime errors [OK]
Common Mistakes:
  • Expecting the array to change
  • Thinking freeze allows modifications
  • Confusing runtime error with syntax error
4. Identify the error in this Ruby code snippet:
name = "Alice"
name.freeze
name[0] = 'M'
puts name
medium
A. No error, output is "Mlice"
B. RuntimeError: can't modify frozen String
C. SyntaxError due to invalid assignment
D. TypeError: wrong type of argument

Solution

  1. Step 1: Recognize string freezing

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

    Trying to change the first character with name[0] = 'M' modifies the string, which is forbidden and raises a runtime error.
  3. Final Answer:

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

    Changing frozen string = RuntimeError [OK]
Hint: Changing frozen strings causes runtime errors [OK]
Common Mistakes:
  • Assuming strings can be changed after freeze
  • Confusing syntax error with runtime error
  • Ignoring freeze effect on string elements
5. Given this Ruby code, what will be the output?
hash = {a: [1, 2], b: [3, 4]}
hash.freeze
hash[:a] << 5
puts hash[:a].inspect
What does this tell us about freezing objects?
hard
A. RuntimeError: can't modify frozen Hash
B. [1, 2] - freeze prevents any changes including nested objects
C. [1, 2, 5] - freeze prevents changing the hash keys but not the objects inside
D. SyntaxError due to invalid hash modification

Solution

  1. Step 1: Understand freezing the hash

    The hash object is frozen, so its structure (keys and values) cannot be changed directly.
  2. Step 2: Analyze modification of nested array

    The value for key :a is an array, which is not frozen. Modifying this array with << 5 is allowed.
  3. Step 3: Observe the output

    The array now contains [1, 2, 5], showing that freezing the hash does not freeze nested objects.
  4. Final Answer:

    [1, 2, 5] - freeze prevents changing the hash keys but not the objects inside -> Option C
  5. Quick Check:

    Freeze is shallow = nested objects can change [OK]
Hint: Freeze is shallow; nested objects stay mutable unless frozen [OK]
Common Mistakes:
  • Assuming freeze affects nested objects
  • Expecting runtime error on nested modification
  • Confusing hash freezing with deep freeze
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