Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a static method in an interface.
Java
public interface Calculator {
static int [1](int a, int b) {
return a + b;
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using a non-descriptive method name.
Forgetting to declare the method as static.
β Incorrect
The method name add is used as the static method name in the interface.
2fill in blank
mediumComplete the code to call the static method from the interface.
Java
int result = Calculator.[1](5, 3);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Trying to call the method on an instance.
Using a wrong method name.
β Incorrect
The static method add is called using the interface name.
3fill in blank
hardFix the error in the static method declaration inside the interface.
Java
public interface Printer {
[1] void print(String message) {
System.out.println(message);
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using 'default' instead of 'static'.
Omitting the 'static' keyword.
β Incorrect
The method must be declared static to be a static method in an interface.
4fill in blank
hardFill both blanks to define and call a static method in an interface.
Java
public interface Utils {
static String [1](String s) {
return s.toUpperCase();
}
}
String result = Utils.[2]("hello"); Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using different names in declaration and call.
Using non-descriptive method names.
β Incorrect
The static method name toUpperCase is used both in declaration and call.
5fill in blank
hardFill all three blanks to create a static method in an interface that returns the square of a number and call it.
Java
public interface MathOps {
static int [1](int n) {
return n [2] n;
}
}
int squared = MathOps.[3](4); Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using '+' instead of '*' for multiplication.
Using different method names in declaration and call.
β Incorrect
The method square multiplies the input by n using the '*' operator and is called as MathOps.square(4).