Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q4 of 15
Ruby - Methods
What will be the output of this Ruby code?
def positive?(num)
  num > 0
end

puts positive?(5)
puts positive?(-3)
A5 -3
Btrue false
Cfalse true
DError: undefined method
Step-by-Step Solution
Solution:
  1. Step 1: Understand method logic

    The method returns true if num is greater than 0, false otherwise.
  2. Step 2: Evaluate calls

    positive?(5) returns true because 5 > 0; positive?(-3) returns false because -3 is not > 0.
  3. Final Answer:

    true\nfalse -> Option B
  4. Quick Check:

    Condition num > 0 returns true or false [OK]
Quick Trick: Predicate methods return true/false based on condition [OK]
Common Mistakes:
  • Expecting numeric output
  • Confusing true/false order
  • Assuming method prints input

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes