C. Using string key instead of symbol key causes nil output
D. Syntax error due to missing curly braces
Solution
Step 1: Check hash keys and access method
The hash uses symbol keys (:theme), but the code tries to access with a string key ('theme').
Step 2: Understand the result of wrong key type
Accessing with a string key returns nil because the keys don't match.
Final Answer:
Using string key instead of symbol key causes nil output -> Option C
Quick Check:
Symbol keys need symbol access [OK]
Hint: Match key types exactly when accessing hashes [OK]
Common Mistakes:
Mixing string and symbol keys
Assuming keys auto-convert types
Ignoring nil return on missing keys
5. You want to store user info with keys :name, :age, and :email. Which hash usage best supports easy updates and clear code?
hard
A. user = { name: 'Eve', age: 28, email: 'eve@example.com' }
B. user = [ 'name' => 'Eve', 'age' => 28, 'email' => 'eve@example.com' ]
C. user = { 'name', 'Eve', 'age', 28, 'email', 'eve@example.com' }
D. user = ( name: 'Eve', age: 28, email: 'eve@example.com' )
Solution
Step 1: Identify correct hash syntax for storing key-value pairs
user = { name: 'Eve', age: 28, email: 'eve@example.com' } uses a hash with symbol keys and values inside curly braces, which is clear and easy to update.
Step 2: Evaluate other options for correctness and clarity
user = [ 'name' => 'Eve', 'age' => 28, 'email' => 'eve@example.com' ] uses array syntax incorrectly, C is invalid syntax, and D uses parentheses which is not valid for hashes.
Final Answer:
user = { name: 'Eve', age: 28, email: 'eve@example.com' } -> Option A
Quick Check:
Curly braces with symbol keys = best practice [OK]
Hint: Use curly braces and symbol keys for clear, update-friendly hashes [OK]
Common Mistakes:
Using arrays instead of hashes
Incorrect syntax with parentheses
Mixing key-value pairs without proper separators
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.