Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a static method inside the class.
Python
class MyClass: @staticmethod def greet(): print([1]) MyClass.greet()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using self or cls as parameters in static methods.
Trying to access instance variables inside static methods.
✗ Incorrect
Static methods do not take self or cls as parameters. They behave like regular functions inside the class. Here, the method prints a string.
2fill in blank
mediumComplete the code to call the static method correctly from an instance.
Python
class Calculator: @staticmethod def add(a, b): return a + b calc = Calculator() result = calc.[1](5, 7) print(result)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to call static method using class name without parentheses.
Using incorrect method names.
✗ Incorrect
Static methods can be called from an instance using the method name. Here, calc.add(5, 7) calls the static method add.
3fill in blank
hardFix the error in the static method definition to make it work.
Python
class Utils: @staticmethod def multiply([1], b): return a * b print(Utils.multiply(3, 4))
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using self or cls as the first parameter in static methods.
Not defining parameters needed for the method.
✗ Incorrect
Static methods do not receive self or cls automatically. You must define all parameters explicitly. Here, 'a' is the first parameter.
4fill in blank
hardFill both blanks to create a static method that returns the square of a number.
Python
class MathOps: @staticmethod def square([1]): return [2] * [1] print(MathOps.square(6))
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using self or cls as parameters in static methods.
Using different variable names inside the method.
✗ Incorrect
The static method takes one parameter 'num' and returns its square by multiplying it by itself.
5fill in blank
hardFill all three blanks to create a static method that filters and returns even numbers from a list.
Python
class Filter: @staticmethod def evens([1]): return [[2] for [3] in numbers if [2] % 2 == 0] print(Filter.evens([1, 2, 3, 4, 5, 6]))
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using self as a parameter in static methods.
Using inconsistent variable names inside the list comprehension.
✗ Incorrect
The static method takes a list 'numbers', iterates over it with 'num', and returns a list of even numbers.