Bird
0
0

Given this class hierarchy:

hard📝 Application Q9 of 15
Ruby - Variables and Data Types
Given this class hierarchy:
class Vehicle; end
class Car < Vehicle; end
class Truck < Vehicle; end

obj = Car.new
Which expression returns true only if obj is exactly a Car, not a subclass?
Aobj.instance_of?(Car)
Bobj.is_a?(Car)
Cobj.class == Vehicle
Dobj.kind_of?(Vehicle)
Step-by-Step Solution
Solution:
  1. Step 1: Understand exact class check

    .instance_of?(Car) returns true only if obj is exactly Car, not subclass.
  2. Step 2: Compare with other options

    .is_a?(Car) and .kind_of?(Vehicle) return true for subclasses; obj.class == Vehicle is false here.
  3. Final Answer:

    obj.instance_of?(Car) -> Option A
  4. Quick Check:

    Use .instance_of? for exact class match [OK]
Quick Trick: Use .instance_of? to exclude subclasses [OK]
Common Mistakes:
  • Using .is_a? for exact match
  • Confusing Vehicle and Car classes
  • Using .class == Vehicle incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes