Bird
0
0

What will be the output of the following Ruby code?

medium📝 Predict Output Q4 of 15
Ruby - Class Methods and Variables
What will be the output of the following Ruby code?
arr = [1, 2, 3]
frozen_arr = arr.freeze
copy = frozen_arr.clone
puts copy.frozen?
Atrue
Bfalse
Cnil
DRuntimeError
Step-by-Step Solution
Solution:
  1. Step 1: Understand freeze behavior

    The array arr is frozen using freeze, making it immutable.
  2. Step 2: Use of clone

    The clone method copies the frozen state of the object, so copy is also frozen.
  3. Step 3: Check frozen state

    copy.frozen? returns true because the frozen state is preserved by clone.
  4. Final Answer:

    true -> Option A
  5. Quick Check:

    Clone copies frozen state [OK]
Quick Trick: clone copies frozen state, dup does not [OK]
Common Mistakes:
  • Assuming dup copies frozen state
  • Expecting false because copy is a new object
  • Confusing frozen? with frozen state copying

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes