Bird
0
0

Consider this Ruby code:

hard📝 Application Q9 of 15
Ruby - Inheritance
Consider this Ruby code:
class Parent
  def process(data)
    data.map { |x| x * 2 }
  end
end

class Child < Parent
  def process(data)
    super(data.select { |x| x.even? })
  end
end

p Child.new.process([1,2,3,4])

What is the output?
A[4, 8]
B[2, 4, 6, 8]
C[1, 3]
DError: wrong number of arguments
Step-by-Step Solution
Solution:
  1. Step 1: Analyze Child's process method

    Child filters input array to only even numbers: [2,4].
  2. Step 2: Call Parent's process with filtered data

    Parent doubles each element: [2*2, 4*2] = [4, 8].
  3. Final Answer:

    [4, 8] -> Option A
  4. Quick Check:

    super called with filtered data returns doubled even numbers [OK]
Quick Trick: super can be called with modified arguments like filtered arrays [OK]
Common Mistakes:
  • Assuming original array is processed
  • Confusing map and select results
  • Expecting error due to argument passing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes