Bird
0
0

Given this Ruby code, what will be the output?

hard📝 Application Q9 of 15
Ruby - Variables and Data Types
Given this Ruby code, what will be the output?
values = [1, 2.5, 3, 4.0]
sum = 0
values.each { |v| sum += v }
puts sum.class
AInteger
BArray
CString
DFloat
Step-by-Step Solution
Solution:
  1. Step 1: Analyze array elements

    Array contains both integers and floats.
  2. Step 2: Sum operation with mixed types

    Adding integers and floats results in float sum in Ruby.
  3. Final Answer:

    sum is a Float. -> Option D
  4. Quick Check:

    Mixed numeric sum results in Float [OK]
Quick Trick: Sum with floats results in float type [OK]
Common Mistakes:
  • Assuming sum is Integer because of some integers
  • Thinking sum is Array or String
  • Ignoring float presence in array

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes