Bird
0
0

What will this Ruby code print?

medium📝 Predict Output Q5 of 15
Ruby - Hashes
What will this Ruby code print?
h = {a: 1, b: 2}
h.each { |k, v| puts "#{k}:#{v}" }
A[:a, 1] [:b, 2]
B1:a 2:b
Ca:1 b:2
Da b
Step-by-Step Solution
Solution:
  1. Step 1: Understand each block parameters

    The block receives key and value as k and v.
  2. Step 2: Analyze the puts statement

    It prints key and value separated by a colon, each pair on a new line.
  3. Final Answer:

    a:1 b:2 -> Option C
  4. Quick Check:

    each yields key and value pairs [OK]
Quick Trick: each yields key and value to block [OK]
Common Mistakes:
MISTAKES
  • Swapping key and value
  • Printing arrays instead of strings
  • Printing only keys or values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes