0
0
Javaprogramming~20 mins

Interface declaration in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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.