Bird
0
0

Find the problem in this Ruby method:

medium📝 Debug Q7 of 15
Ruby - Methods
Find the problem in this Ruby method:
def check(value)
  if value > 0
    return
  else
    return "Negative"
  end
end
AMethod returns boolean values
BSyntax error due to missing return value
CMethod always returns string
DMethod returns nil for positive values
Step-by-Step Solution
Solution:
  1. Step 1: Analyze return statements for each condition

    For value > 0, method returns without value, which means nil.
  2. Step 2: Check else branch return value

    Else branch returns string "Negative".
  3. Final Answer:

    Method returns nil for positive values -> Option D
  4. Quick Check:

    Return without value returns nil [OK]
Quick Trick: Return without value returns nil, be explicit if needed [OK]
Common Mistakes:
  • Assuming return without value returns last expression
  • Expecting string always returned
  • Confusing nil with false

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes