0
0
Rubyprogramming~5 mins

Symbol type and immutability in Ruby - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a Symbol in Ruby?
A Symbol is a lightweight, immutable identifier used mainly as names or keys. It is unique and stored only once in memory.
Click to reveal answer
beginner
Why are Symbols considered immutable in Ruby?
Symbols cannot be changed after creation. This immutability means their value and identity stay constant throughout the program.
Click to reveal answer
intermediate
How do Symbols differ from Strings in Ruby?
Symbols are immutable and stored once, making them memory-efficient for repeated use. Strings are mutable and can have many copies in memory.
Click to reveal answer
intermediate
What happens if you create the same Symbol multiple times?
Ruby reuses the same Symbol object for identical names, so all references point to the same memory location.
Click to reveal answer
beginner
Give an example of creating a Symbol in Ruby.
You create a Symbol by prefixing a name with a colon, like :example. This creates a Symbol named 'example'.
Click to reveal answer
Which of the following is a Symbol in Ruby?
A'my_symbol'
B"my_symbol"
Cmy_symbol
D:my_symbol
What is true about Symbols in Ruby?
AThey are immutable and unique.
BThey are mutable and can be changed.
CThey consume more memory than strings.
DThey are only used for numbers.
If you create :apple twice in Ruby, what happens?
ATwo different Symbol objects are created.
BAn error occurs.
CThe same Symbol object is reused.
DA new String is created instead.
Which is a benefit of using Symbols over Strings as keys in hashes?
ASymbols use less memory and are faster.
BSymbols can be changed anytime.
CSymbols are mutable.
DSymbols are slower to compare.
How do you create a Symbol named 'status' in Ruby?
A"status"
B:status
C'status'
Dstatus
Explain what a Symbol is in Ruby and why it is immutable.
Think about how Symbols are used as names or keys.
You got /3 concepts.
    Describe the difference between Symbols and Strings in Ruby.
    Consider memory use and mutability.
    You got /4 concepts.