Bird
0
0

Consider this Ruby code snippet:

medium📝 Debug Q14 of 15
Ruby - Class Methods and Variables
Consider this Ruby code snippet:
str = "hello"
str.freeze
copy = str.dup
puts copy.frozen?
What is the output and why? If there is an error, identify and fix it.
Atrue
Bfalse
CNoMethodError
DRuntimeError
Step-by-Step Solution
Solution:
  1. Step 1: Understand freezing and dup behavior

    The string str is frozen. When dup is called, it copies the object but does NOT copy the frozen state.
  2. Step 2: Check frozen state of copy

    Since dup skips frozen state, copy.frozen? returns false.
  3. Final Answer:

    false -> Option B
  4. Quick Check:

    dup skips frozen state, so frozen? = false [OK]
Quick Trick: dup skips frozen state, clone keeps it [OK]
Common Mistakes:
  • Expecting copy to be frozen
  • Confusing frozen? method with freeze
  • Assuming dup raises error on frozen objects

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes