Bird
0
0

You want to calculate the average of three numbers: 7, 8, and 9 in Ruby. Which expression correctly computes the average as a float?

hard📝 Application Q15 of 15
Ruby - Operators and Expressions
You want to calculate the average of three numbers: 7, 8, and 9 in Ruby. Which expression correctly computes the average as a float?
A7 + 8 + 9 / 3
B(7 + 8 + 9) / 3
C(7 + 8 + 9) / 3.0
D(7 + 8 + 9) % 3
Step-by-Step Solution
Solution:
  1. Step 1: Calculate sum of numbers

    Sum is 7 + 8 + 9 = 24.
  2. Step 2: Divide by 3 as float division

    Dividing by 3.0 forces float division, giving 8.0 instead of integer 8.
  3. Final Answer:

    (7 + 8 + 9) / 3.0 -> Option C
  4. Quick Check:

    Use float divisor for float average [OK]
Quick Trick: Divide by float (3.0) to get decimal average [OK]
Common Mistakes:
  • Using integer division losing decimals
  • Misusing % instead of /
  • Wrong operator precedence without parentheses

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes