0
0
Javaprogramming~20 mins

Static methods in interfaces in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Interface Static Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of calling static method in interface
What is the output of the following Java code?
Java
interface Calculator {
    static int add(int a, int b) {
        return a + b;
    }
}

public class Test {
    public static void main(String[] args) {
        System.out.println(Calculator.add(5, 7));
    }
}
A12
BCompilation error: cannot call static method in interface
CRuntime error
D0
Attempts:
2 left
πŸ’‘ Hint
Static methods in interfaces can be called using the interface name.
🧠 Conceptual
intermediate
1:30remaining
Where can static methods in interfaces be called from?
Which of the following is true about calling static methods defined in a Java interface?
AThey can be called using the interface name anywhere in the code.
BThey can only be called from within the interface itself.
CThey can be called using an instance of a class implementing the interface.
DThey cannot be called at all; static methods are not allowed in interfaces.
Attempts:
2 left
πŸ’‘ Hint
Think about how static methods are accessed in classes.
πŸ”§ Debug
advanced
2:30remaining
Identify the error in static method usage
What error will this code produce?
Java
interface Printer {
    static void print() {
        System.out.println("Printing from interface");
    }
}

class Document implements Printer {
    public void print() {
        System.out.println("Printing from class");
    }
}

public class Test {
    public static void main(String[] args) {
        Document doc = new Document();
        doc.print();
        Printer.print();
        doc.Printer.print();
    }
}
ACompilation error at Printer.print();
BCompilation error at doc.Printer.print();
CRuntime error at doc.print();
D
No errors; prints:
Printing from class
Printing from interface
Printing from interface
Attempts:
2 left
πŸ’‘ Hint
Check how static methods in interfaces are accessed.
πŸ“ Syntax
advanced
1:30remaining
Valid syntax for static method in interface
Which option correctly declares a static method inside a Java interface?
Avoid static show() { System.out.println("Hello"); }
Bstatic void show();
Cstatic void show() { System.out.println("Hello"); }
Dpublic static void show();
Attempts:
2 left
πŸ’‘ Hint
Static methods in interfaces must have a body.
πŸš€ Application
expert
2:30remaining
Using static methods in interfaces for utility
Given this interface, what will be the output of the main method?
Java
interface Utils {
    static String greet(String name) {
        return "Hello, " + name + "!";
    }
}

public class Main {
    public static void main(String[] args) {
        System.out.println(Utils.greet("Alice"));
        String message = Utils.greet(null);
        System.out.println(message == null ? "No greeting" : message);
    }
}
ACompilation error
B
Hello, Alice!
No greeting
C
Hello, Alice!
NullPointerException
D
Hello, Alice!
Hello, null!
Attempts:
2 left
πŸ’‘ Hint
What happens when you concatenate a string with null in Java?