Challenge - 5 Problems
Interface Static Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediate2: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)); } }
Attempts:
2 left
π‘ Hint
Static methods in interfaces can be called using the interface name.
β Incorrect
Static methods defined in interfaces can be called using the interface name directly. Here, Calculator.add(5, 7) returns 12.
π§ Conceptual
intermediate1: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?
Attempts:
2 left
π‘ Hint
Think about how static methods are accessed in classes.
β Incorrect
Static methods in interfaces are accessed using the interface name, not through instances or implementing classes.
π§ Debug
advanced2: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(); } }
Attempts:
2 left
π‘ Hint
Check how static methods in interfaces are accessed.
β Incorrect
Static methods in interfaces cannot be called through an instance or implementing class reference. The line doc.Printer.print(); causes a compilation error.
π Syntax
advanced1:30remaining
Valid syntax for static method in interface
Which option correctly declares a static method inside a Java interface?
Attempts:
2 left
π‘ Hint
Static methods in interfaces must have a body.
β Incorrect
Static methods in interfaces must be declared with a body. Option C is correct syntax. Options C and D lack method bodies, causing errors. Option C has wrong keyword order.
π Application
expert2: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); } }
Attempts:
2 left
π‘ Hint
What happens when you concatenate a string with null in Java?
β Incorrect
Concatenating a string with null results in the string "null". So Utils.greet(null) returns "Hello, null!".