Challenge - 5 Problems
Master of Methods with Parameters
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of method with default and positional parameters
What is the output of this Python code?
Python
class Calculator: def multiply(self, x, y=2): return x * y calc = Calculator() print(calc.multiply(3))
Attempts:
2 left
💡 Hint
Remember that y has a default value of 2 if not provided.
✗ Incorrect
The method multiply takes x and y, with y defaulting to 2. Calling multiply(3) uses y=2, so 3*2=6.
❓ Predict Output
intermediate2:00remaining
Output when method parameters are passed by keyword
What will this code print?
Python
class Greeter: def greet(self, name, greeting='Hello'): print(f"{greeting}, {name}!") g = Greeter() g.greet(greeting='Hi', name='Alice')
Attempts:
2 left
💡 Hint
Check how keyword arguments match parameters.
✗ Incorrect
The greet method is called with greeting='Hi' and name='Alice', so it prints 'Hi, Alice!'.
❓ Predict Output
advanced2:00remaining
Output of method with mutable default parameter
What is the output of this code?
Python
class Collector: def __init__(self): self.items = [] def add(self, item, collection=[]): collection.append(item) return collection c = Collector() print(c.add('apple')) print(c.add('banana'))
Attempts:
2 left
💡 Hint
Default mutable parameters keep their state between calls.
✗ Incorrect
The default list 'collection' is shared between calls, so the second call appends 'banana' to the same list containing 'apple'.
❓ Predict Output
advanced2:00remaining
Output of method using *args and **kwargs
What does this code print?
Python
class Printer: def show(self, *args, **kwargs): print(args) print(kwargs) p = Printer() p.show(1, 2, a=3, b=4)
Attempts:
2 left
💡 Hint
*args collects positional arguments as a tuple, **kwargs collects keyword arguments as a dict.
✗ Incorrect
The method prints the tuple of positional arguments and the dictionary of keyword arguments.
🧠 Conceptual
expert3:00remaining
Understanding parameter passing and object mutation
Consider this code. What is the output after calling modify_list?
Python
def modify_list(lst): lst.append(4) lst = [1, 2, 3] return lst my_list = [0] result = modify_list(my_list) print(my_list) print(result)
Attempts:
2 left
💡 Hint
Think about how lists are passed by reference and reassignment inside the function.
✗ Incorrect
The original list 'my_list' is modified by append (so it becomes [0,4]). Then inside the function, lst is reassigned to a new list [1,2,3], which does not affect 'my_list'. The function returns the new list.