Bird
0
0

Identify the problem in this code:

medium📝 Debug Q7 of 15
Ruby - Enumerable and Collection Processing
Identify the problem in this code:
arr = [1, 2, 3]
result = arr.inject(0) { |sum, n| sum * n }
puts result
AMissing return statement in block
BBlock parameters are incorrect
Cinject cannot be used with multiplication
DInitial value 0 causes product to always be 0
Step-by-Step Solution
Solution:
  1. Step 1: Analyze initial value and operation

    Starting with 0 and multiplying by any number results in 0 always.
  2. Step 2: Understand effect on result

    Result will always be 0, which is likely unintended for product calculation.
  3. Final Answer:

    Initial value 0 causes product to always be 0 -> Option D
  4. Quick Check:

    Multiplying from 0 yields 0 always [OK]
Quick Trick: Use 1 as initial for multiplication, not 0 [OK]
Common Mistakes:
  • Using 0 as initial for product
  • Assuming inject can't multiply

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes