Bird
0
0

Identify the error in this Ruby code:

medium📝 Debug Q14 of 15
Ruby - Methods
Identify the error in this Ruby code:
def positive?(num)
  if num > 0
    true
  else
    false
  end
end

puts positive?("5")
AArgumentError because "5" is a string, not a number
BSyntaxError due to missing end
CNo error, outputs true
DReturns nil instead of true/false
Step-by-Step Solution
Solution:
  1. Step 1: Analyze method input and comparison

    The method compares num > 0, but "5" is a string, not a number.
  2. Step 2: Understand Ruby type comparison

    Comparing string "5" with integer 0 causes an ArgumentError at runtime.
  3. Final Answer:

    ArgumentError because "5" is a string, not a number -> Option A
  4. Quick Check:

    Comparing string and integer causes ArgumentError [OK]
Quick Trick: Check input types before numeric comparisons [OK]
Common Mistakes:
  • Passing strings instead of numbers
  • Assuming no error on mixed type comparisons
  • Missing end keyword (not the case here)

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes