Challenge - 5 Problems
Functional OOP Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of combining functional and OOP styles
What is the output of this Ruby code that mixes functional style with OOP?
Ruby
class Calculator def initialize(numbers) @numbers = numbers end def double_and_sum @numbers.map { |n| n * 2 }.reduce(0, :+) end end calc = Calculator.new([1, 2, 3]) puts calc.double_and_sum
Attempts:
2 left
💡 Hint
Think about how map and reduce work on arrays inside the class.
✗ Incorrect
The array [1, 2, 3] is doubled element-wise to [2, 4, 6], then summed to 12.
🧠 Conceptual
intermediate1:30remaining
Why use functional patterns in OOP?
Which reason best explains why functional programming patterns complement object-oriented programming?
Attempts:
2 left
💡 Hint
Think about how pure functions behave compared to methods with side effects.
✗ Incorrect
Functional patterns encourage pure functions which reduce side effects, making code easier to test and maintain within OOP.
🔧 Debug
advanced2:00remaining
Identify the error mixing functional and OOP styles
What error does this Ruby code produce when mixing functional and OOP styles?
Ruby
class DataProcessor def initialize(data) @data = data end def process @data.map! { |x| x * 2 } @data.reduce(:+) end end processor = DataProcessor.new(10) puts processor.process
Attempts:
2 left
💡 Hint
Check the type of @data when calling map! inside the class.
✗ Incorrect
The code tries to call map! on an Integer, which is invalid because map! is an Array method.
📝 Syntax
advanced2:00remaining
Correct functional method syntax in OOP context
Which option correctly defines a method in Ruby that uses a functional style inside a class?
Ruby
class Transformer def initialize(values) @values = values end def transform # Fill in the blank end end
Attempts:
2 left
💡 Hint
Functional style prefers returning new collections without modifying originals.
✗ Incorrect
Option B returns a new array with each value incremented, following functional style without side effects.
🚀 Application
expert3:00remaining
Predict the final output combining OOP and functional methods
Given this Ruby code combining OOP and functional patterns, what is the final output?
Ruby
class Stats def initialize(numbers) @numbers = numbers end def mean @numbers.reduce(0.0, :+) / @numbers.size end def squared_diffs @numbers.map { |n| (n - mean) ** 2 } end def variance squared_diffs.reduce(0.0, :+) / @numbers.size end end stats = Stats.new([2, 4, 4, 4, 5, 5, 7, 9]) puts stats.variance.round(2)
Attempts:
2 left
💡 Hint
Calculate mean first, then squared differences, then average those.
✗ Incorrect
The variance calculation follows the formula: average of squared differences from the mean, resulting in 4.0.