Bird
0
0

Which of the following Ruby method definitions returns nil?

easy📝 Predict Output Q3 of 15
Ruby - Methods
Which of the following Ruby method definitions returns nil?
Adef multiply(x, y) x * y end
Bdef multiply(x, y) x * y return x * y end
Cdef multiply(x, y) x * y return end
Ddef multiply(x, y) x * y return x * y end
Step-by-Step Solution
Solution:
  1. Step 1: Analyze each method for return value

    def multiply(x, y) x * y end returns x * y implicitly. def multiply(x, y) x * y return end returns nil because return without value returns nil. def multiply(x, y) x * y return x * y end returns x * y explicitly. def multiply(x, y) x * y return x * y end returns x * y explicitly.
  2. Step 2: Identify the one that returns nil

    def multiply(x, y) x * y return end returns nil due to the empty return.
  3. Final Answer:

    The method with empty return -> Option C
  4. Quick Check:

    Empty return returns nil [OK]
Quick Trick: Empty return returns nil [OK]
Common Mistakes:
  • Assuming empty return causes syntax error
  • Confusing return without value with error
  • Ignoring Ruby's flexible return syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes