0
0
Javaprogramming~10 mins

Static methods in interfaces in Java - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Aadd
Bsum
Ccalculate
Dcompute
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using a non-descriptive method name.
Forgetting to declare the method as static.
2fill in blank
medium

Complete 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'
Acompute
Badd
Csum
Dcalculate
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Trying to call the method on an instance.
Using a wrong method name.
3fill in blank
hard

Fix 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'
Adefault
Bprivate
Cstatic
Dpublic
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'default' instead of 'static'.
Omitting the 'static' keyword.
4fill in blank
hard

Fill 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'
AtoUpperCase
Bconvert
Cuppercase
DmakeUpper
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using different names in declaration and call.
Using non-descriptive method names.
5fill in blank
hard

Fill 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'
Asquare
B*
D+
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using '+' instead of '*' for multiplication.
Using different method names in declaration and call.