Bird
0
0

Why does Ruby freeze string literals by default in some versions, and what is the benefit?

hard📝 Conceptual Q10 of 15
Ruby - String Operations
Why does Ruby freeze string literals by default in some versions, and what is the benefit?
ATo prevent strings from being printed accidentally.
BTo improve performance and reduce memory usage by reusing immutable strings.
CTo allow strings to be modified faster.
DTo convert all strings to symbols automatically.
Step-by-Step Solution
Solution:
  1. Step 1: Understand frozen string literals feature

    Ruby can freeze string literals to make them immutable by default.
  2. Step 2: Identify benefits

    This reduces memory usage by reusing the same string object and improves performance by avoiding unnecessary copies.
  3. Final Answer:

    To improve performance and reduce memory usage by reusing immutable strings. -> Option B
  4. Quick Check:

    Frozen literals optimize memory and speed [OK]
Quick Trick: Frozen literals save memory and speed up code [OK]
Common Mistakes:
MISTAKES
  • Thinking freezing prevents printing
  • Believing freezing speeds up modifications
  • Confusing strings with symbols

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes