Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q4 of 15
Ruby - Variables and Data Types
What will be the output of this Ruby code?
num1 = 7
num2 = 2
result = num1 / num2
puts result
A3.5
B3
C4
D3.0
Step-by-Step Solution
Solution:
  1. Step 1: Identify types of num1 and num2

    Both num1 and num2 are integers (7 and 2).
  2. Step 2: Integer division in Ruby

    Dividing two integers with / returns an integer result by truncating decimals, so 7 / 2 = 3.
  3. Final Answer:

    The output is 3. -> Option B
  4. Quick Check:

    Integer division truncates decimals = 3 [OK]
Quick Trick: Integer division truncates decimals in Ruby [OK]
Common Mistakes:
  • Expecting 3.5 as output from integer division
  • Confusing integer division with float division
  • Assuming automatic float conversion

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes