Bird
0
0

Identify the error in this Ruby code:

medium📝 Debug Q14 of 15
Ruby - Variables and Data Types
Identify the error in this Ruby code:
def multiply(x, y)
  x * y
end

result = multiply(3, "4")
puts result
ANameError for undefined variable
BSyntaxError due to method definition
CNo error, outputs 12
DTypeError due to multiplying integer and string
Step-by-Step Solution
Solution:
  1. Step 1: Check method parameters and call

    The method multiply is called with an integer (3) and a string ("4").
  2. Step 2: Understand Ruby's type rules for *

    Multiplying integer by string without conversion causes a TypeError in Ruby.
  3. Final Answer:

    TypeError due to multiplying integer and string -> Option D
  4. Quick Check:

    Strong typing blocks int * string = A [OK]
Quick Trick: Check operand types for * operator to avoid TypeError [OK]
Common Mistakes:
  • Thinking Ruby auto-converts string to integer
  • Assuming no error on mixed types
  • Confusing SyntaxError with TypeError

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes