Bird
Raised Fist0
Javaprogramming~20 mins

Interface declaration 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 Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of interface method call
What is the output of this Java program that uses an interface and a class implementing it?
Java
interface Greet {
    String sayHello();
}

class EnglishGreeting implements Greet {
    public String sayHello() {
        return "Hello!";
    }
}

public class Main {
    public static void main(String[] args) {
        Greet greet = new EnglishGreeting();
        System.out.println(greet.sayHello());
    }
}
ARuntime error
BsayHello
CCompilation error
DHello!
Attempts:
2 left
πŸ’‘ Hint
Look at how the interface method is implemented and called.
🧠 Conceptual
intermediate
1:30remaining
Interface method modifiers
Which of the following statements about methods declared in a Java interface is true?
AInterface methods are implicitly public and abstract unless declared default or static.
BInterface methods cannot be overridden by implementing classes.
CInterface methods must always have a body.
DInterface methods can be private by default.
Attempts:
2 left
πŸ’‘ Hint
Think about the default visibility and abstract nature of interface methods.
πŸ”§ Debug
advanced
2:00remaining
Identify the compilation error in interface implementation
What error will this code produce when compiled?
Java
interface Calculator {
    int add(int a, int b);
}

class SimpleCalculator implements Calculator {
    public int add(int x, int y) {
        return x + y;
    }
    public int subtract(int a, int b) {
        return a - b;
    }
}

public class Main {
    public static void main(String[] args) {
        Calculator calc = new SimpleCalculator();
        System.out.println(calc.add(5, 3));
    }
}
ACompilation error: method add must override interface method
BNo compilation error; program prints 8
CCompilation error: subtract method must be implemented
DRuntime error: ClassCastException
Attempts:
2 left
πŸ’‘ Hint
Check if the method signatures match between interface and class.
πŸ“ Syntax
advanced
1:30remaining
Correct interface declaration syntax
Which option shows the correct way to declare an interface with one method in Java?
Ainterface Worker { void work(); }
Binterface Worker { void work() {} }
Cinterface Worker { public void work() {} }
Dinterface Worker { abstract void work() {} }
Attempts:
2 left
πŸ’‘ Hint
Interface methods do not have a body unless default or static.
πŸš€ Application
expert
2:30remaining
Interface with multiple inheritance
Given these interfaces and classes, what will be the output when running the main method?
Java
interface A {
    default String message() { return "A"; }
}
interface B {
    default String message() { return "B"; }
}
class C implements A, B {
    public String message() {
        return A.super.message() + B.super.message();
    }
}
public class Main {
    public static void main(String[] args) {
        C obj = new C();
        System.out.println(obj.message());
    }
}
ACompilation error due to conflicting default methods
BBA
CAB
DRuntime error
Attempts:
2 left
πŸ’‘ Hint
Look at how class C resolves the conflict by calling both interface methods explicitly.

Practice

(1/5)
1. What is the main purpose of an interface in Java?
easy
A. To inherit code from multiple classes
B. To store data values like variables
C. To create objects directly
D. To declare methods that a class must implement without providing their body

Solution

  1. Step 1: Understand what an interface declares

    An interface only declares method signatures without any implementation.
  2. Step 2: Compare with other options

    Interfaces do not store data, create objects, or inherit code from classes.
  3. Final Answer:

    To declare methods that a class must implement without providing their body -> Option D
  4. Quick Check:

    Interface purpose = declare methods only [OK]
Hint: Interfaces declare methods without bodies [OK]
Common Mistakes:
  • Thinking interfaces can store variables
  • Confusing interfaces with classes
  • Believing interfaces create objects
2. Which of the following is the correct way to declare an interface named Vehicle in Java?
easy
A. interface Vehicle {}
B. class Vehicle interface {}
C. interface Vehicle() {}
D. Vehicle interface {}

Solution

  1. Step 1: Recall Java interface syntax

    Interfaces are declared with the keyword interface followed by the name and curly braces.
  2. Step 2: Check each option

    interface Vehicle {} matches correct syntax: interface Vehicle {}. Others have syntax errors like misplaced parentheses or keywords.
  3. Final Answer:

    interface Vehicle {} -> Option A
  4. Quick Check:

    Correct interface syntax = interface Name {} [OK]
Hint: Use 'interface Name {}' to declare interfaces [OK]
Common Mistakes:
  • Adding parentheses after interface name
  • Mixing class and interface keywords
  • Omitting the 'interface' keyword
3. What will be the output of the following code?
interface Animal {
    void sound();
}

class Dog implements Animal {
    public void sound() {
        System.out.println("Bark");
    }
}

public class Test {
    public static void main(String[] args) {
        Animal a = new Dog();
        a.sound();
    }
}
medium
A. Compilation error
B. sound
C. Bark
D. No output

Solution

  1. Step 1: Understand interface implementation

    The class Dog implements Animal and provides the method sound() which prints "Bark".
  2. Step 2: Trace the main method execution

    In main, an Animal reference points to a Dog object, calling sound() prints "Bark".
  3. Final Answer:

    Bark -> Option C
  4. Quick Check:

    Interface method called prints 'Bark' [OK]
Hint: Implemented method runs when called via interface reference [OK]
Common Mistakes:
  • Expecting interface to print something
  • Missing 'public' in method implementation causing error
  • Thinking no output occurs
4. Identify the error in this interface declaration:
interface Calculator {
    int add(int a, int b) {
        return a + b;
    }
}
medium
A. Interfaces cannot have method bodies unless default or static
B. Method name 'add' is invalid in interfaces
C. Return type 'int' is not allowed in interfaces
D. Interface name must start with lowercase

Solution

  1. Step 1: Check method body rules in interfaces

    In Java, interface methods cannot have bodies unless marked as default or static.
  2. Step 2: Analyze the given method

    The method add has a body but no default or static keyword, causing a syntax error.
  3. Final Answer:

    Interfaces cannot have method bodies unless default or static -> Option A
  4. Quick Check:

    Method bodies in interfaces need default/static [OK]
Hint: Interface methods need default/static for bodies [OK]
Common Mistakes:
  • Adding method bodies without default/static
  • Thinking method names are restricted
  • Ignoring Java naming conventions
5. Given two interfaces:
interface Printable {
    void print();
}

interface Showable {
    void show();
}

class Document implements Printable, Showable {
    public void print() {
        System.out.println("Printing document");
    }
    public void show() {
        System.out.println("Showing document");
    }
}

public class Test {
    public static void main(String[] args) {
        Document doc = new Document();
        doc.print();
        doc.show();
    }
}

What is the output when running Test?
hard
A. Compilation error due to multiple interfaces
B. Printing document\nShowing document
C. Printing document only
D. No output

Solution

  1. Step 1: Understand multiple interface implementation

    Java allows a class to implement multiple interfaces and must provide all their methods.
  2. Step 2: Trace method calls in main

    The Document class implements both methods. Calling print() and show() prints both messages.
  3. Final Answer:

    Printing document\nShowing document -> Option B
  4. Quick Check:

    Multiple interfaces implemented, all methods run [OK]
Hint: Classes can implement many interfaces, all methods must be defined [OK]
Common Mistakes:
  • Thinking multiple interfaces cause errors
  • Forgetting to implement all interface methods
  • Expecting only one method to run