Bird
0
0

What will this Ruby method return?

medium📝 Predict Output Q5 of 15
Ruby - Methods
What will this Ruby method return?
def check
  if false
    'yes'
  else
    'no'
  end
end
p check
Anil
B"yes"
C"no"
Dfalse
Step-by-Step Solution
Solution:
  1. Step 1: Evaluate the if-else condition

    The condition is false, so the else branch executes, returning 'no'.
  2. Step 2: Understand method return

    The method returns the last evaluated expression, which is 'no'. The p prints "no" including quotes.
  3. Final Answer:

    "no" -> Option C
  4. Quick Check:

    Method returns else branch value = "no" [OK]
Quick Trick: If-else returns last evaluated branch value [OK]
Common Mistakes:
  • Assuming method returns nil if condition false
  • Confusing false with string 'no'
  • Ignoring else branch return

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes