Bird
0
0

What is the output of the following Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - Operators and Expressions
What is the output of the following Ruby code?
result = 15 / 4
puts result
A3
BSyntaxError
C4
D3.75
Step-by-Step Solution
Solution:
  1. Step 1: Understand integer division in Ruby

    When dividing two integers, Ruby performs integer division, discarding the decimal part.
  2. Step 2: Calculate 15 divided by 4 as integer division

    15 / 4 equals 3.75, but integer division returns 3.
  3. Final Answer:

    3 -> Option A
  4. Quick Check:

    Integer division truncates decimals = 3 [OK]
Quick Trick: Integer division drops decimals unless floats used [OK]
Common Mistakes:
  • Expecting float result without floats
  • Confusing / with %
  • Thinking output is 4 or 3.75

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes