Bird
0
0

What is the output of this Ruby code?

medium📝 Predict Output Q4 of 15
Ruby - File IO

What is the output of this Ruby code?

require 'json'
json_str = '{"name":"Bob","age":25}'
obj = JSON.parse(json_str)
puts obj["name"]
ABob
B25
C{"name":"Bob","age":25}
Dnil
Step-by-Step Solution
Solution:
  1. Step 1: Parse JSON string into Ruby hash

    JSON.parse converts the JSON string into a Ruby hash with keys as strings.
  2. Step 2: Access the value for key "name"

    obj["name"] returns the value associated with "name", which is "Bob".
  3. Final Answer:

    Bob -> Option A
  4. Quick Check:

    Access parsed JSON key = "Bob" [OK]
Quick Trick: Parsed JSON keys are strings, access with string keys [OK]
Common Mistakes:
  • Accessing with symbol keys instead of strings
  • Printing the whole JSON string
  • Expecting numeric output for name key

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes