Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - Class Methods and Variables
What will be the output of this Ruby code?
arr = [1, 2, 3]
arr.freeze
arr << 4
puts arr.inspect
ARuntimeError: can't modify frozen Array
B[1, 2, 3, 4]
C[1, 2, 3]
DSyntaxError
Step-by-Step Solution
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]
Quick Trick: Modifying frozen objects causes runtime errors [OK]
Common Mistakes:
  • Expecting the array to change
  • Thinking freeze allows modifications
  • Confusing runtime error with syntax error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes