Bird
0
0

Given keys = [:a, :b, :c] and values = [1, 2], what happens when you run keys.zip(values).to_h?

hard📝 Application Q9 of 15
Ruby - Enumerable and Collection Processing
Given keys = [:a, :b, :c] and values = [1, 2], what happens when you run keys.zip(values).to_h?
ACreates {:a=>1, :b=>2, :c=>nil} with missing values as nil
BRaises an error because arrays differ in length
CCreates {:a=>1, :b=>2} ignoring extra keys
DCreates {:a=>1, :b=>2, :c=>0} filling missing with zero
Step-by-Step Solution
Solution:
  1. Step 1: Zip keys and values arrays

    Since values is shorter, zip fills missing with nil.
  2. Step 2: Convert zipped array to hash

    Hash includes key :c with value nil.
  3. Final Answer:

    {:a=>1, :b=>2, :c=>nil} with missing values as nil -> Option A
  4. Quick Check:

    Zip fills missing with nil, to_h creates hash [OK]
Quick Trick: Zip fills missing with nil before to_h [OK]
Common Mistakes:
  • Expecting error on length mismatch
  • Assuming missing keys are dropped
  • Assuming missing values filled with zero

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes