Bird
0
0

What will be the result when this Ruby code is executed?

medium📝 Predict Output Q5 of 15
Ruby - Variables and Data Types
What will be the result when this Ruby code is executed?
def divide(a, b)
  a / b
end

puts divide(10, "2")
ATypeError: String can't be coerced into Integer
B5
C2
D0
Step-by-Step Solution
Solution:
  1. Step 1: Understand the code

    The method divides two parameters a and b.
  2. Step 2: Analyze the arguments

    Arguments are 10 (Integer) and "2" (String). Ruby does not automatically convert string to integer in arithmetic operations.
  3. Step 3: Predict behavior

    Ruby raises a TypeError when trying to divide Integer by String.
  4. Final Answer:

    TypeError: String can't be coerced into Integer -> Option A
  5. Quick Check:

    Ruby enforces strong typing in operations [OK]
Quick Trick: Ruby does not auto-convert string to number in math [OK]
Common Mistakes:
  • Assuming implicit string to integer conversion
  • Expecting output '5' or '2' without error
  • Ignoring Ruby's strong typing in operations

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes