Bird
0
0

Find the bug in this code:

medium📝 Debug Q7 of 15
Ruby - Inheritance
Find the bug in this code:
class Fruit; end
class Apple < Fruit; end
obj = Apple.new
if obj.kind_of? Fruit
  puts "Yes"
else
  puts "No"
end
ANo bug, code works correctly
BIncorrect inheritance between Apple and Fruit
CMissing parentheses in kind_of? call
Dkind_of? should be replaced with instance_of?
Step-by-Step Solution
Solution:
  1. Step 1: Check method call syntax

    In Ruby, parentheses are optional for kind_of? method calls with arguments; obj.kind_of? Fruit is valid.
  2. Step 2: Identify if there's a bug

    Inheritance is correct (Apple < Fruit), so condition is true, prints "Yes". No bug.
  3. Final Answer:

    No bug, code works correctly -> Option A
  4. Quick Check:

    Parentheses optional for kind_of? [OK]
Quick Trick: Parentheses optional with kind_of? and is_a? [OK]
Common Mistakes:
  • Thinking parentheses are required
  • Confusing kind_of? with instance_of?
  • Assuming inheritance is wrong

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes