0
0
Rubyprogramming~20 mins

Symbol type and immutability in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Symbol Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Ruby code using symbols?
Consider the following Ruby code snippet. What will it print?
Ruby
sym1 = :hello
sym2 = :hello
puts sym1.object_id == sym2.object_id
ASyntaxError
Btrue
Cfalse
Dnil
Attempts:
2 left
💡 Hint
Symbols with the same name share the same object in memory.
Predict Output
intermediate
2:00remaining
What happens when you try to modify a symbol?
What error will this Ruby code produce?
Ruby
sym = :example
sym[0] = 'E'
ANoMethodError: undefined method `[]=' for Symbol
BRuntimeError: can't modify symbol
CTypeError: no implicit conversion of String into Integer
DSyntaxError
Attempts:
2 left
💡 Hint
Symbols do not support string-like modification methods.
🧠 Conceptual
advanced
2:00remaining
Why are symbols considered immutable in Ruby?
Which of the following best explains why symbols are immutable?
ABecause Ruby automatically freezes all objects of type Symbol
BBecause symbols are stored as strings internally and strings are immutable
CBecause symbols are unique and shared, changing one would affect all references
DBecause symbols are garbage collected immediately after creation
Attempts:
2 left
💡 Hint
Think about what would happen if you changed a symbol's value.
Predict Output
advanced
2:00remaining
What is the output of this code comparing symbols and strings?
What will this Ruby code print?
Ruby
puts :ruby == 'ruby'
puts :ruby.eql?('ruby')
A
false
false
B
true
true
C
false
true
D
true
false
Attempts:
2 left
💡 Hint
Symbols and strings are different types even if they look similar.
Predict Output
expert
2:00remaining
How many unique objects are created in this Ruby code?
How many unique objects are created after running this code?
Ruby
arr = [:a, :b, :a, :c, :b]
unique = arr.uniq
puts unique.size
A2
B5
C4
D3
Attempts:
2 left
💡 Hint
uniq removes duplicates from the array.