Bird
0
0

Which of the following expressions correctly checks if obj is an instance of class String or its subclass?

easy📝 Conceptual Q2 of 15
Ruby - Inheritance
Which of the following expressions correctly checks if obj is an instance of class String or its subclass?
Aobj.is_a?(String)
Bobj.kind_of?(Integer)
Cobj.instance_of?(String)
Dobj.is_a?(Array)
Step-by-Step Solution
Solution:
  1. Step 1: Identify the correct method and class

    To check if obj is a String or subclass, use is_a? with String.
  2. Step 2: Verify options

    Only obj.is_a?(String) uses is_a? with String. Others check wrong classes or use instance_of? which does not check subclasses.
  3. Final Answer:

    obj.is_a?(String) -> Option A
  4. Quick Check:

    Check subclass with is_a? = obj.is_a?(String) [OK]
Quick Trick: Use is_a? to check class or subclass membership [OK]
Common Mistakes:
  • Using instance_of? which excludes subclasses
  • Checking wrong class
  • Confusing kind_of? with instance_of?

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes