Bird
0
0

What will be the output of the following Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - Modules and Mixins
What will be the output of the following Ruby code?
module MathTools
  def self.square(x)
    x * x
  end
end

puts MathTools.square(4)
A16
Bsquare
CError: undefined method
D4
Step-by-Step Solution
Solution:
  1. Step 1: Understand the method definition

    The method square is defined as a module method using self.square. It returns the square of the input.
  2. Step 2: Calculate the output

    Calling MathTools.square(4) returns 4 * 4 = 16, which is printed.
  3. Final Answer:

    16 -> Option A
  4. Quick Check:

    4 squared = 16 [OK]
Quick Trick: Multiply input by itself in module method [OK]
Common Mistakes:
  • Forgetting self and causing undefined method error
  • Confusing output with input
  • Expecting method name as output

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes