Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - Variables and Data Types
What will be the output of this Ruby code?
def add(a, b)
  a + b
end

puts add(5, "10")
A15
BTypeError
C510
Dnil
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the method call

    The method add tries to add an integer (5) and a string ("10") using + operator.
  2. Step 2: Understand Ruby's strong typing

    Ruby does not automatically convert types for +; adding integer and string causes a TypeError.
  3. Final Answer:

    TypeError -> Option B
  4. Quick Check:

    Strong typing prevents int + string = B [OK]
Quick Trick: Adding different types without conversion causes TypeError [OK]
Common Mistakes:
MISTAKES
  • Assuming automatic string conversion
  • Expecting concatenation of numbers and strings
  • Ignoring Ruby's type safety rules

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes