Bird
Raised Fist0
Javaprogramming~20 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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?

Practice

(1/5)
1. What is the correct way to call a static method calculate() defined inside an interface MathOps?
easy
A. MathOps.calculate()
B. new MathOps().calculate()
C. calculate()
D. MathOps obj = new MathOps(); obj.calculate()

Solution

  1. Step 1: Understand static method call in interfaces

    Static methods in interfaces are called using the interface name, not instances.
  2. Step 2: Analyze the options

    Only MathOps.calculate() correctly calls the static method. Creating instances or calling directly is invalid.
  3. Final Answer:

    MathOps.calculate() -> Option A
  4. Quick Check:

    Static method call = InterfaceName.method() [OK]
Hint: Call static interface methods with InterfaceName.method() [OK]
Common Mistakes:
  • Trying to call static method on an instance
  • Calling static method without interface name
  • Trying to instantiate an interface
2. Which of the following is the correct syntax to declare a static method printMessage inside an interface Logger?
easy
A. static void printMessage();
B. static void printMessage() { System.out.println("Hello"); }
C. public static void printMessage();
D. void static printMessage() { System.out.println("Hello"); }

Solution

  1. Step 1: Recall static method syntax in interfaces

    Static methods must have a body and use static keyword before return type.
  2. Step 2: Check each option

    Only static void printMessage() { System.out.println("Hello"); } correctly declares and defines the static method. Declarations without a body or with static after the return type are invalid.
  3. Final Answer:

    static void printMessage() { System.out.println("Hello"); } -> Option B
  4. Quick Check:

    Static method = static + return type + name + () + body [OK]
Hint: Static methods in interfaces need a body and static keyword first [OK]
Common Mistakes:
  • Omitting method body in static method
  • Placing static keyword after return type
  • Declaring static methods without body
3. What will be the output of the following code?
interface Helper {
    static String greet() {
        return "Hi!";
    }
}
public class Test {
    public static void main(String[] args) {
        System.out.println(Helper.greet());
    }
}
medium
A. Runtime error
B. Compile-time error
C. null
D. Hi!

Solution

  1. Step 1: Understand static method call in interface

    The static method greet() is called correctly using Helper.greet().
  2. Step 2: Predict output

    The method returns "Hi!" which is printed by System.out.println.
  3. Final Answer:

    Hi! -> Option D
  4. Quick Check:

    Static method returns "Hi!" printed [OK]
Hint: Static interface methods run when called by InterfaceName.method() [OK]
Common Mistakes:
  • Trying to call static method on instance
  • Expecting compile error due to interface method
  • Confusing static with default methods
4. Identify the error in the following code snippet:
interface Calculator {
    static int add(int a, int b) {
        return a + b;
    }
}

public class Demo {
    public static void main(String[] args) {
        Calculator calc = new Calculator();
        System.out.println(calc.add(5, 3));
    }
}
medium
A. Missing return statement in add()
B. Static method add() cannot be called on instance
C. Cannot instantiate interface Calculator
D. No error, code runs fine

Solution

  1. Step 1: Check interface instantiation

    Interfaces cannot be instantiated directly using new.
  2. Step 2: Analyze method call

    Static methods must be called using interface name, not instance. But the main error is instantiating interface.
  3. Final Answer:

    Cannot instantiate interface Calculator -> Option C
  4. Quick Check:

    Interfaces cannot be instantiated [OK]
Hint: Interfaces cannot be created with new keyword [OK]
Common Mistakes:
  • Trying to instantiate interface
  • Calling static method on instance
  • Ignoring compile errors on interface instantiation
5. Given the interface Utils with a static method isEven(int n) that returns true if n is even, how can you use this method inside a class NumberChecker to filter even numbers from a list List<Integer> nums using streams?
hard
A. nums.stream().filter(Utils::isEven).toList();
B. nums.stream().filter(n -> Utils.isEven()).toList();
C. nums.stream().filter(n -> isEven(n)).toList();
D. nums.stream().filter(n -> Utils.isEven(n)).collect();

Solution

  1. Step 1: Understand method reference syntax

    Static methods can be referenced as InterfaceName::methodName in streams.
  2. Step 2: Analyze options for correct syntax

    nums.stream().filter(Utils::isEven).toList(); uses method reference correctly. The lambda n -> Utils.isEven() misses argument n, n -> isEven(n) lacks interface qualification, and .collect() requires a collector.
  3. Final Answer:

    nums.stream().filter(Utils::isEven).toList(); -> Option A
  4. Quick Check:

    Use InterfaceName::staticMethod for stream filters [OK]
Hint: Use InterfaceName::methodName for static method references in streams [OK]
Common Mistakes:
  • Calling static method without argument in lambda
  • Using instance method syntax for static methods
  • Wrong terminal operation like collect() without collector