Challenge - 5 Problems
Ruby Access Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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]Attempts:
2 left
💡 Hint
Look at how the hash is accessed and what key is used in puts.
✗ Incorrect
The hash initially has keys :a and :b with values 1 and 2. We add :c with value 3. Then we print the value for key :b, which is 2.
❓ Predict Output
intermediate2: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]Attempts:
2 left
💡 Hint
Check how the nested hash value is updated before printing.
✗ Incorrect
The nested hash under :user has :age initially 30. It is updated to 31, so printing it outputs 31.
❓ Predict Output
advanced2: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.lengthAttempts:
2 left
💡 Hint
Think about what happens when accessing a missing key in a hash and calling to_s on nil.
✗ Incorrect
Accessing hash[:c] returns nil since :c is missing. Calling to_s on nil returns an empty string "". Its length is 0.
❓ Predict Output
advanced2: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
Attempts:
2 left
💡 Hint
Remember how Ruby arrays handle assignment beyond current length.
✗ Incorrect
Assigning arr[1] = 5 changes second element to 5. Assigning arr[3] = 7 adds a new element at index 3. The array becomes [1, 5, 3, 7].
🧠 Conceptual
expert2: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]Attempts:
2 left
💡 Hint
Consider the type of value[:x] and how to get the number 42 printed.
✗ Incorrect
Option C adds 2 to the integer 40, resulting in 42. Printing value[:x] outputs 42. Option C sets it to string '42' but printing will output '42' as string, which looks the same but is not the integer 42. Option C converts 42 to string, also prints '42' as string. Option C causes a TypeError because you cannot add integer and string.