Bird
Raised Fist0
Rubyprogramming~10 mins

Why single inheritance in Ruby - Test Your Understanding

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a class that inherits from another class in Ruby.

Ruby
class ChildClass < [1]
  def greet
    puts 'Hello from child'
  end
end
Drag options to blanks, or click blank then click option'
AParentClass
BChildClass
CObject
DModule
Attempts:
3 left
💡 Hint
Common Mistakes
Using the child class name instead of the parent class name after <.
Trying to inherit from a module using <.
2fill in blank
medium

Complete the code to call the parent class method from the child class in Ruby.

Ruby
class Parent
  def greet
    puts 'Hello from parent'
  end
end

class Child < Parent
  def greet
    [1]
    puts 'Hello from child'
  end
end
Drag options to blanks, or click blank then click option'
Asuper.greet
Bself.greet
CParent.greet
Dsuper
Attempts:
3 left
💡 Hint
Common Mistakes
Calling Parent.greet directly, which is incorrect syntax.
Using self.greet which causes infinite recursion.
3fill in blank
hard

Fix the error in the code to correctly inherit and override a method in Ruby.

Ruby
class Animal
  def speak
    puts 'Animal sound'
  end
end

class Dog [1] Animal
  def speak
    puts 'Bark'
  end
end
Drag options to blanks, or click blank then click option'
A>
B<
C=
Dinherits
Attempts:
3 left
💡 Hint
Common Mistakes
Using > or = instead of < for inheritance.
Writing 'inherits' as a keyword, which Ruby does not support.
4fill in blank
hard

Fill both blanks to create a class that inherits from a parent and calls the parent's method in Ruby.

Ruby
class Vehicle
  def start
    puts 'Vehicle started'
  end
end

class Car [1] Vehicle
  def start
    [2]
    puts 'Car started'
  end
end
Drag options to blanks, or click blank then click option'
A<
Bsuper
Cself
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using > instead of < for inheritance.
Using self instead of super to call the parent method.
5fill in blank
hard

Fill all three blanks to define a parent class, a child class inheriting it, and call the parent's method in Ruby.

Ruby
class [1]
  def info
    puts 'Parent info'
  end
end

class [2] < [3]
  def info
    super
    puts 'Child info'
  end
end
Drag options to blanks, or click blank then click option'
AParent
BChild
DAnimal
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for the parent class in definition and inheritance.
Not calling super to invoke the parent's method.

Practice

(1/5)
1. Why does Ruby use single inheritance instead of multiple inheritance?
easy
A. To keep code simple and avoid conflicts between parent classes
B. Because Ruby does not support any form of inheritance
C. To allow a class to inherit from many classes at once
D. To make the language slower and more complex

Solution

  1. Step 1: Understand Ruby's inheritance model

    Ruby allows only one parent class for inheritance, which is called single inheritance.
  2. Step 2: Reason why single inheritance is chosen

    This design avoids conflicts that can happen when inheriting from multiple classes and keeps code easier to understand and maintain.
  3. Final Answer:

    To keep code simple and avoid conflicts between parent classes -> Option A
  4. Quick Check:

    Single inheritance avoids conflicts = D [OK]
Hint: Single inheritance avoids confusion and keeps code clean [OK]
Common Mistakes:
  • Thinking Ruby supports multiple inheritance directly
  • Believing inheritance makes code slower
  • Confusing modules with classes in inheritance
2. Which of the following is the correct way to declare a class inheriting from another class in Ruby?
easy
A. class ChildClass : ParentClass; end
B. class ChildClass inherits ParentClass; end
C. class ChildClass extends ParentClass; end
D. class ChildClass < ParentClass; end

Solution

  1. Step 1: Recall Ruby class inheritance syntax

    In Ruby, inheritance is declared using the less-than symbol (<) between class names.
  2. Step 2: Match syntax with options

    Only class ChildClass < ParentClass; end uses the correct Ruby syntax: class ChildClass < ParentClass; end.
  3. Final Answer:

    class ChildClass < ParentClass; end -> Option D
  4. Quick Check:

    Ruby inheritance uses < symbol = C [OK]
Hint: Use < symbol to inherit a class in Ruby [OK]
Common Mistakes:
  • Using 'inherits' keyword which Ruby does not have
  • Using ':' or 'extends' like other languages
  • Forgetting the < symbol for inheritance
3. What will be the output of this Ruby code?
class A
  def greet
    'Hello from A'
  end
end

class B < A
end

b = B.new
puts b.greet
medium
A. Hello from B
B. NoMethodError
C. Hello from A
D. SyntaxError

Solution

  1. Step 1: Understand inheritance in the code

    Class B inherits from class A but does not override the greet method.
  2. Step 2: Determine method call result

    Since B does not have greet, Ruby looks in A and finds greet returning 'Hello from A'.
  3. Final Answer:

    Hello from A -> Option C
  4. Quick Check:

    Inherited method runs = B [OK]
Hint: Inherited methods run if not overridden [OK]
Common Mistakes:
  • Expecting 'Hello from B' without method override
  • Thinking NoMethodError occurs if subclass lacks method
  • Confusing syntax errors with inheritance behavior
4. Identify the error in this Ruby code that tries to inherit from two classes:
class Parent1
end

class Parent2
end

class Child < Parent1, Parent2
end
medium
A. No error, multiple inheritance is allowed
B. SyntaxError because Ruby does not support multiple inheritance
C. RuntimeError due to ambiguous method calls
D. NameError because Parent2 is undefined

Solution

  1. Step 1: Check Ruby inheritance rules

    Ruby supports only single inheritance, so a class cannot inherit from two classes at once.
  2. Step 2: Analyze the code error

    The syntax class Child < Parent1, Parent2 is invalid and causes a syntax error.
  3. Final Answer:

    SyntaxError because Ruby does not support multiple inheritance -> Option B
  4. Quick Check:

    Multiple inheritance causes syntax error = A [OK]
Hint: Ruby allows only one parent class, multiple inheritance is syntax error [OK]
Common Mistakes:
  • Assuming Ruby supports multiple inheritance like some other languages
  • Confusing runtime errors with syntax errors
  • Thinking Parent2 is undefined when it is declared
5. How can Ruby add features from multiple sources to a class despite single inheritance? Choose the best explanation.
hard
A. By using modules that can be mixed in with 'include' or 'extend' to add methods
B. By inheriting from multiple classes separated by commas
C. By copying code manually from other classes into one class
D. By using special keywords to override single inheritance

Solution

  1. Step 1: Understand Ruby's way to add multiple behaviors

    Ruby uses modules to add extra methods to classes without multiple inheritance.
  2. Step 2: Identify how modules are used

    Modules are mixed in with include or extend keywords to add features.
  3. Final Answer:

    By using modules that can be mixed in with 'include' or 'extend' to add methods -> Option A
  4. Quick Check:

    Modules add features despite single inheritance = A [OK]
Hint: Use modules with include/extend to add features [OK]
Common Mistakes:
  • Thinking multiple inheritance is possible with commas
  • Believing manual copying is the Ruby way
  • Expecting special keywords to override inheritance rules
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard