Challenge - 5 Problems
Method Invocation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of chained method calls
What is the output of this Python code involving chained method calls?
Python
class TextProcessor: def __init__(self, text): self.text = text def to_upper(self): self.text = self.text.upper() return self def add_exclamation(self): self.text += '!' return self def get_text(self): return self.text result = TextProcessor('hello').to_upper().add_exclamation().get_text() print(result)
Attempts:
2 left
💡 Hint
Look at how each method modifies the text and returns self for chaining.
✗ Incorrect
The methods to_upper and add_exclamation both modify the text and return the object itself, allowing chaining. Finally, get_text returns the modified string 'HELLO!'.
❓ Predict Output
intermediate2:00remaining
Method call order and output
What will be printed when this code runs?
Python
class Counter: def __init__(self): self.count = 0 def increment(self): self.count += 1 return self def double(self): self.count *= 2 return self def get(self): return self.count c = Counter() print(c.increment().double().increment().get())
Attempts:
2 left
💡 Hint
Trace the count value step by step through each method call.
✗ Incorrect
Starting at 0: increment() makes 1, double() makes 2, increment() makes 3, get() returns 3.
🔧 Debug
advanced2:00remaining
Identify the error in method chaining
This code tries to chain methods but raises an error. What is the error type?
Python
class Builder: def __init__(self): self.value = '' def add_a(self): self.value += 'a' def add_b(self): self.value += 'b' def get_value(self): return self.value b = Builder() print(b.add_a().add_b().get_value())
Attempts:
2 left
💡 Hint
Check what each method returns and how chaining works.
✗ Incorrect
Methods add_a and add_b do not return self, so add_a() returns None. Then None.add_b() causes an AttributeError: 'NoneType' object has no attribute 'add_b'.
🧠 Conceptual
advanced2:00remaining
Understanding method invocation flow with inheritance
Given these classes, what will be the output of the code below?
Python
class Base: def action(self): return 'Base' class Child(Base): def action(self): return 'Child' + super().action() obj = Child() print(obj.action())
Attempts:
2 left
💡 Hint
Look at how super() calls the parent method inside the child method.
✗ Incorrect
Child's action method returns 'Child' plus the result of Base's action method, which is 'Base', so the output is 'ChildBase'.
❓ Predict Output
expert3:00remaining
Complex method invocation with side effects
What is the final output of this code?
Python
class Tracker: def __init__(self): self.log = [] def step1(self): self.log.append('1') return self def step2(self): self.log.append('2') return self def step3(self): self.log.append('3') return self def get_log(self): return ''.join(self.log) tracker = Tracker() result = tracker.step1().step2().step1().step3().get_log() print(result)
Attempts:
2 left
💡 Hint
Follow the order of method calls and what each appends to the log.
✗ Incorrect
The calls append '1', then '2', then '1' again, then '3', so the log is '1213'.