Bird
0
0

Why does Ruby return an Integer when dividing two integers but a Float when dividing an Integer by a Float?

hard📝 Conceptual Q10 of 15
Ruby - Variables and Data Types
Why does Ruby return an Integer when dividing two integers but a Float when dividing an Integer by a Float?
ARuby always returns Integer regardless of operand types.
BRuby converts all numbers to strings before division.
CRuby performs integer division if both operands are integers, else float division.
DRuby throws an error if operands are different types.
Step-by-Step Solution
Solution:
  1. Step 1: Understand Ruby division rules

    When both operands are integers, Ruby performs integer division, truncating decimals.
  2. Step 2: Mixed type division behavior

    If either operand is a float, Ruby performs float division and returns a float result.
  3. Final Answer:

    Ruby performs integer division if both operands are integers, else float division. -> Option C
  4. Quick Check:

    Division type depends on operand types [OK]
Quick Trick: Division result type depends on operand types [OK]
Common Mistakes:
MISTAKES
  • Thinking Ruby always returns Integer
  • Assuming automatic string conversion
  • Expecting error on mixed types

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes