Bird
0
0

What is the output of this Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - File IO

What is the output of this Ruby code?

require 'json'
json_str = '{"age":30,"city":"NY"}'
ruby_obj = JSON.parse(json_str)
puts ruby_obj["city"]
ASyntaxError
B30
Cnil
DNY
Step-by-Step Solution
Solution:
  1. Step 1: Parse JSON string into Ruby hash

    The string {"age":30,"city":"NY"} is parsed into a Ruby hash with keys "age" and "city".
  2. Step 2: Access the value for key "city"

    Using ruby_obj["city"] returns the string "NY".
  3. Final Answer:

    NY -> Option D
  4. Quick Check:

    JSON.parse + key access = value [OK]
Quick Trick: Parse JSON then access keys like a hash [OK]
Common Mistakes:
  • Expecting numeric output instead of string
  • Using symbol keys instead of string keys
  • Forgetting to require 'json'

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes