Bird
0
0

You want to write a Kotlin function that accepts any number type (Int, Double, Float) without worrying about primitives. How does Kotlin's type system help you?

hard📝 Application Q8 of 15
Kotlin - Data Types
You want to write a Kotlin function that accepts any number type (Int, Double, Float) without worrying about primitives. How does Kotlin's type system help you?
ABy forcing you to write separate functions for each primitive type
BBy using a unified Number class that all numeric types inherit from
CBy requiring explicit casting to primitive types
DBy disallowing mixed numeric types in functions
Step-by-Step Solution
Solution:
  1. Step 1: Recall Kotlin's numeric type hierarchy

    All numeric types inherit from the Number class, which is an object type.
  2. Step 2: Understand function parameter flexibility

    You can accept Number type to handle Int, Double, Float uniformly without primitives.
  3. Final Answer:

    By using a unified Number class that all numeric types inherit from -> Option B
  4. Quick Check:

    Unified Number class enables generic numeric functions [OK]
Quick Trick: Use Number type to accept all numeric values [OK]
Common Mistakes:
MISTAKES
  • Thinking separate functions are needed
  • Assuming explicit primitive casting is required
  • Believing mixed types are disallowed

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes