Bird
0
0

Given this method, what will be the return value?

hard📝 Application Q15 of 15
Ruby - Methods
Given this method, what will be the return value?
def check_value(val)
  if val > 10
    "High"
  else
    "Low"
  end
end

check_value(10)
A"High"
B"Low"
Cnil
DError
Step-by-Step Solution
Solution:
  1. Step 1: Evaluate the condition with val = 10

    Since 10 is not greater than 10, the else branch executes returning "Low".
  2. Step 2: Understand implicit return in if-else

    The last evaluated expression is the string "Low", which is implicitly returned by the method.
  3. Final Answer:

    "Low" -> Option B
  4. Quick Check:

    Implicit return of last expression in if-else = "Low" [OK]
Quick Trick: If-else returns last expression evaluated implicitly [OK]
Common Mistakes:
  • Assuming val > 10 includes 10
  • Expecting explicit return needed
  • Confusing nil with else return

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes