Bird
0
0

Which of the following is a disadvantage of using string keys instead of symbol keys in Ruby hashes?

easy📝 Conceptual Q2 of 15
Ruby - Hashes
Which of the following is a disadvantage of using string keys instead of symbol keys in Ruby hashes?
AString keys are immutable and cannot be changed.
BString keys are faster to compare than symbol keys.
CString keys automatically convert to symbols internally.
DString keys consume more memory when reused multiple times.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze string key behavior

    String keys are mutable and each instance is a separate object, so reusing the same string key creates multiple objects.
  2. Step 2: Understand memory impact

    This leads to higher memory consumption compared to symbols, which are single immutable objects.
  3. Final Answer:

    String keys consume more memory when reused multiple times. -> Option D
  4. Quick Check:

    String keys memory usage = C [OK]
Quick Trick: Strings use more memory if repeated as keys; symbols don't [OK]
Common Mistakes:
  • Confusing immutability of strings and symbols
  • Assuming string keys convert to symbols automatically
  • Believing string keys are always faster

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes