Bird
0
0

What is wrong with this code snippet?

medium📝 Debug Q7 of 15
Ruby - Functional Patterns in Ruby
What is wrong with this code snippet?
arr = [1, 2, 3]
arr.freeze
arr.push(4)
puts arr.inspect
ASyntaxError due to push method
BRuntimeError because push modifies frozen array
COutput is [1, 2, 3, 4]
DOutput is [1, 2, 3]
Step-by-Step Solution
Solution:
  1. Step 1: Freeze the array

    arr.freeze makes the array immutable.
  2. Step 2: push attempts to modify array

    arr.push(4) tries to add an element, causing RuntimeError.
  3. Final Answer:

    RuntimeError because push modifies frozen array -> Option B
  4. Quick Check:

    freeze + push = RuntimeError [OK]
Quick Trick: Modifying frozen arrays with push raises RuntimeError [OK]
Common Mistakes:
  • Expecting push to work silently
  • Confusing syntax error with runtime error
  • Assuming output includes new element

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes