Bird
0
0

You want to calculate the average of three integers a, b, and c in Kotlin. Which expression correctly calculates the average as a Double?

hard📝 Application Q8 of 15
Kotlin - Operators and Expressions
You want to calculate the average of three integers a, b, and c in Kotlin. Which expression correctly calculates the average as a Double?
A(a + b + c) / 3.0
B(a + b + c) / 3
Ca + b + c / 3
D(a + b + c) % 3
Step-by-Step Solution
Solution:
  1. Step 1: Understand integer vs floating division

    Dividing by 3 (an integer) causes integer division, losing decimals.
  2. Step 2: Use floating point divisor for decimal result

    Dividing by 3.0 (Double) forces floating point division, giving a Double average.
  3. Final Answer:

    (a + b + c) / 3.0 -> Option A
  4. Quick Check:

    Use floating divisor for decimal average [OK]
Quick Trick: Divide by 3.0 for decimal average, not 3 [OK]
Common Mistakes:
MISTAKES
  • Using integer division losing decimals
  • Misplacing parentheses
  • Using modulus instead of division

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes