Bird
0
0

Which of the following is the correct syntax to check if variable obj is exactly an instance of class String?

easy📝 Syntax Q3 of 15
Ruby - Variables and Data Types
Which of the following is the correct syntax to check if variable obj is exactly an instance of class String?
Aobj.is_a?(String)
Bobj.instance_of?(String)
Cobj.class(String)
Dobj.kind_of?(String)
Step-by-Step Solution
Solution:
  1. Step 1: Understand exact class check

    .instance_of? returns true only if the object is exactly of the given class, not subclass.
  2. Step 2: Compare with other options

    .is_a? and .kind_of? allow subclasses; .class == String works but is less idiomatic.
  3. Final Answer:

    obj.instance_of?(String) -> Option B
  4. Quick Check:

    Exact class check uses .instance_of? [OK]
Quick Trick: Use .instance_of? for exact class match [OK]
Common Mistakes:
MISTAKES
  • Using .is_a? for exact match
  • Comparing .class with == but ignoring subclasses
  • Confusing .kind_of? with .instance_of?

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes