Bird
0
0

What will be the output of the following Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - Hashes
What will be the output of the following Ruby code?
h = {a: 1, b: 2, c: 3}
h.keys
A[:1, :2, :3]
B[1, 2, 3]
C{a: 1, b: 2, c: 3}
D[:a, :b, :c]
Step-by-Step Solution
Solution:
  1. Step 1: Understand keys method on hash

    The keys method returns an array of all keys in the hash. Here keys are symbols :a, :b, :c.
  2. Step 2: Match output with options

    [:a, :b, :c] matches the array of keys. [1, 2, 3] is values, C is the whole hash, D is invalid symbol array.
  3. Final Answer:

    [:a, :b, :c] -> Option D
  4. Quick Check:

    h.keys = [:a, :b, :c] [OK]
Quick Trick: keys returns array of keys exactly as in hash [OK]
Common Mistakes:
MISTAKES
  • Confusing keys with values output
  • Expecting hash instead of array
  • Misreading symbols as strings

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes