Bird
Raised Fist0
Rubyprogramming~20 mins

String freezing for immutability in Ruby - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Ruby String 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 this Ruby code with frozen string?
Consider the following Ruby code snippet. What will it print?
Ruby
str = "hello"
str.freeze
begin
  str << " world"
rescue => e
  puts e.class
end
ATypeError
BFrozenError
Chello world
Dhello
Attempts:
2 left
💡 Hint
Frozen strings cannot be modified. What error does Ruby raise when you try?
🧠 Conceptual
intermediate
1:30remaining
Why freeze a string in Ruby?
Which of the following is the main reason to freeze a string in Ruby?
ATo make the string immutable and prevent accidental changes
BTo convert the string to uppercase automatically
CTo allow the string to be garbage collected faster
DTo enable string interpolation
Attempts:
2 left
💡 Hint
Think about what freezing means in everyday life: making something fixed or unchangeable.
❓ Predict Output
advanced
2:00remaining
What is the output of this Ruby code with frozen string and dup?
What will this Ruby code print?
Ruby
str = "data"
frozen_str = str.freeze
copy = frozen_str.dup
copy << "base"
puts copy
puts frozen_str
A
database
data
B
data
database
C
FrozenError
FrozenError
D
datadata
base
Attempts:
2 left
💡 Hint
Duplicating a frozen string creates a new unfrozen copy.
🔧 Debug
advanced
2:00remaining
Identify the error in this Ruby code using frozen strings
What error will this code produce and why?
Ruby
def add_suffix(s)
  s.freeze
  s << "_end"
end

puts add_suffix("test")
AArgumentError because freeze takes no arguments
BNo error, prints 'test_end'
CTypeError because << is not defined for frozen strings
DFrozenError because the string is frozen before modification
Attempts:
2 left
💡 Hint
Freezing a string means you cannot change it afterwards.
🚀 Application
expert
2:30remaining
How many items are in the resulting array after freezing strings?
Given this Ruby code, how many elements does the array contain after execution?
Ruby
arr = []
3.times do |i|
  s = "item#{i}"
  arr << s.freeze
end
arr << "item3"
arr.freeze
arr << "item4"
A3
B5
CRaises FrozenError
D4
Attempts:
2 left
💡 Hint
What happens if you try to add an element to a frozen array?

Practice

(1/5)
1. What does freezing a string in Ruby do?
str = "hello".freeze
easy
A. Converts the string to uppercase.
B. Deletes the string from memory.
C. Makes the string unchangeable (immutable).
D. Allows the string to be modified faster.

Solution

  1. Step 1: Understand the freeze method

    The freeze method in Ruby makes an object immutable, meaning it cannot be changed after freezing.
  2. Step 2: Apply to strings

    When applied to a string, it prevents any further modifications to that string's content.
  3. Final Answer:

    Makes the string unchangeable (immutable). -> Option C
  4. Quick Check:

    freeze means immutable = A [OK]
Hint: Freeze means no changes allowed to the string [OK]
Common Mistakes:
  • Thinking freeze deletes the string
  • Assuming freeze changes string content
  • Believing freeze speeds up modification
2. Which of the following is the correct way to freeze a string in Ruby?
easy
A. str.freeze = "hello"
B. str = "hello".freeze
C. freeze str = "hello"
D. str = freeze("hello")

Solution

  1. Step 1: Review Ruby freeze syntax

    The correct syntax to freeze a string is to call .freeze on the string object, like "hello".freeze.
  2. Step 2: Check each option

    str = "hello".freeze correctly calls .freeze on the string literal. Options B, C, and D use invalid syntax or incorrect method calls.
  3. Final Answer:

    str = "hello".freeze -> Option B
  4. Quick Check:

    Call .freeze on string = A [OK]
Hint: Use .freeze after the string literal [OK]
Common Mistakes:
  • Trying to assign freeze as a variable
  • Using freeze as a standalone keyword
  • Calling freeze as a function with parentheses
3. What will happen when the following Ruby code runs?
str = "hello".freeze
str << " world"
medium
A. The string becomes "hello world".
B. The string remains "hello" silently.
C. The string changes to " world".
D. A runtime error occurs: can't modify frozen String.

Solution

  1. Step 1: Understand string freezing effect

    Freezing the string "hello" makes it immutable, so any modification attempt will raise an error.
  2. Step 2: Analyze the << operation

    The << operator tries to append " world" to the frozen string, which is not allowed and causes a runtime error.
  3. Final Answer:

    A runtime error occurs: can't modify frozen String. -> Option D
  4. Quick Check:

    Modifying frozen string = error C [OK]
Hint: Appending to frozen string causes error [OK]
Common Mistakes:
  • Assuming string changes silently
  • Thinking string resets to new value
  • Believing no error occurs
4. Identify the error in this Ruby code:
name = "Alice"
name.freeze
name.upcase!
medium
A. Error because upcase! tries to modify a frozen string.
B. No error; the code runs fine.
C. Error because freeze is called incorrectly.
D. Error because name is not a string.

Solution

  1. Step 1: Check freeze usage

    The freeze method is correctly called on the string name, making it immutable.
  2. Step 2: Understand upcase! effect

    The upcase! method tries to change the string in place, which is not allowed on a frozen string, causing a runtime error.
  3. Final Answer:

    Error because upcase! tries to modify a frozen string. -> Option A
  4. Quick Check:

    Modifying frozen string with upcase! = error B [OK]
Hint: In-place methods fail on frozen strings [OK]
Common Mistakes:
  • Thinking freeze is called incorrectly
  • Assuming no error on upcase!
  • Confusing variable type
5. You want to store a constant greeting message that should never change in your Ruby program. Which approach is best?
GREETING = "Welcome!".freeze

Why is freezing important here?
hard
A. It prevents accidental changes to the greeting, ensuring data integrity.
B. It makes the greeting print faster.
C. It allows the greeting to be changed later if needed.
D. It compresses the string to save memory.

Solution

  1. Step 1: Understand constant usage

    Constants like GREETING should not change during program execution to avoid bugs.
  2. Step 2: Role of freeze on constants

    Freezing the string ensures it cannot be modified accidentally, protecting the constant's value.
  3. Final Answer:

    It prevents accidental changes to the greeting, ensuring data integrity. -> Option A
  4. Quick Check:

    Freeze constant for safety = D [OK]
Hint: Freeze constants to keep them safe from changes [OK]
Common Mistakes:
  • Thinking freeze speeds up printing
  • Believing freeze allows changes later
  • Assuming freeze compresses data
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