Bird
0
0

Given two arrays products = ["Pen", "Notebook", "Eraser"] and prices = [1.5, 2.0, 0.5], which Ruby code correctly creates a hash mapping each product to its price using zip?

hard📝 Application Q8 of 15
Ruby - Enumerable and Collection Processing
Given two arrays products = ["Pen", "Notebook", "Eraser"] and prices = [1.5, 2.0, 0.5], which Ruby code correctly creates a hash mapping each product to its price using zip?
Aproducts.zip(prices).to_a
BHash[products.zip(prices)]
Cproducts + prices.to_h
Dproducts.zip(prices).flatten.to_h
Step-by-Step Solution
Solution:
  1. Step 1: Use zip to pair arrays

    products.zip(prices) creates array of pairs.
  2. Step 2: Convert pairs to hash

    Use Hash[] to convert pairs into a hash.
  3. Final Answer:

    Hash[products.zip(prices)] -> Option B
  4. Quick Check:

    Check for hash creation from zipped pairs [OK]
Quick Trick: Use Hash[] with zipped arrays to create hash [OK]
Common Mistakes:
  • Using to_a instead of Hash conversion
  • Trying to add arrays directly
  • Flattening before to_h causing errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes