Bird
0
0

What will this Ruby code print?

medium📝 Predict Output Q5 of 15
Ruby - Variables and Data Types
What will this Ruby code print?
value = 3.14
puts value.class == Float
puts value.is_a?(Numeric)
Afalse false
Bfalse true
Ctrue false
Dtrue true
Step-by-Step Solution
Solution:
  1. Step 1: Check value.class == Float

    The variable value is a Float, so this expression returns true.
  2. Step 2: Check value.is_a?(Numeric)

    Float is a subclass of Numeric, so is_a? returns true.
  3. Final Answer:

    true true -> Option D
  4. Quick Check:

    Float is a Numeric subclass, so both checks are true [OK]
Quick Trick: Float is Numeric subclass, both checks true [OK]
Common Mistakes:
  • Assuming class equality fails for Float
  • Thinking is_a? returns false for superclass

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes