0
0
Rubyprogramming~20 mins

Accessing and setting values in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ruby Access Master
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?
Consider the following Ruby code snippet. What will be printed when it runs?
Ruby
hash = {a: 1, b: 2}
hash[:c] = 3
puts hash[:b]
A2
B3
CKeyError
Dnil
Attempts:
2 left
💡 Hint
Look at how the hash is accessed and what key is used in puts.
Predict Output
intermediate
2:00remaining
What does this code output after setting a nested value?
Look at this Ruby code that sets a nested value in a hash. What will it print?
Ruby
data = {user: {name: "Alice", age: 30}}
data[:user][:age] = 31
puts data[:user][:age]
A30
BNoMethodError
Cnil
D31
Attempts:
2 left
💡 Hint
Check how the nested hash value is updated before printing.
Predict Output
advanced
2:00remaining
What is the output of this Ruby code?
Consider the following Ruby code snippet. What will be printed when it runs?
Ruby
hash = {a: 1, b: 2}
puts hash[:c].to_s.length
A0
BTypeError
CNoMethodError
D1
Attempts:
2 left
💡 Hint
Think about what happens when accessing a missing key in a hash and calling to_s on nil.
Predict Output
advanced
2:00remaining
What is the final value of the variable after setting values?
What will be the value of the variable 'arr' after running this Ruby code?
Ruby
arr = [1, 2, 3]
arr[1] = 5
arr[3] = 7
p arr
A[1, 5, 3]
B[1, 5, 3, 7]
C[1, 5, 7]
D[1, 2, 3, 7]
Attempts:
2 left
💡 Hint
Remember how Ruby arrays handle assignment beyond current length.
🧠 Conceptual
expert
2:00remaining
Which option produces the exact output '42'?
Given the following Ruby code, which option will print exactly '42'?
Ruby
value = {x: 40}
# Your code here
puts value[:x]
Avalue[:x] = 42.to_s
Bvalue[:x] = '42'
Cvalue[:x] += 2
Dvalue[:x] = 40 + '2'
Attempts:
2 left
💡 Hint
Consider the type of value[:x] and how to get the number 42 printed.