0
0
Javaprogramming~20 mins

Implementing interfaces 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 implementation
What is the output of this Java program that implements an interface?
Java
interface Speaker {
    void speak();
}

class Person implements Speaker {
    public void speak() {
        System.out.println("Hello from Person");
    }
}

public class Main {
    public static void main(String[] args) {
        Speaker s = new Person();
        s.speak();
    }
}
ACompilation error: speak() must be public
BHello from Person
CRuntime error: NullPointerException
DNo output
Attempts:
2 left
πŸ’‘ Hint
Check if the method speak() is correctly implemented with the right access modifier.
❓ Predict Output
intermediate
2:00remaining
Interface reference to different implementations
What will be printed when running this Java code?
Java
interface Animal {
    void sound();
}

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

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

public class Main {
    public static void main(String[] args) {
        Animal a = new Dog();
        a.sound();
        a = new Cat();
        a.sound();
    }
}
A
Woof
Meow
B
Woof
Woof
C
Meow
Woof
DCompilation error: cannot assign Cat to Animal
Attempts:
2 left
πŸ’‘ Hint
Look at how the interface reference is assigned to different objects.
πŸ”§ Debug
advanced
2:00remaining
Why does this interface implementation cause a compile error?
Identify the cause of the compile error in this code snippet.
Java
interface Calculator {
    int add(int a, int b);
}

class SimpleCalculator implements Calculator {
    public int add(int x, int y) {
        return x + y;
    }
}
AMethod add() in SimpleCalculator must be declared public
BInterface Calculator cannot have method with parameters
CClass SimpleCalculator must be abstract
DMethod add() must return void
Attempts:
2 left
πŸ’‘ Hint
Check the access modifier of the implemented method compared to the interface method.
πŸ“ Syntax
advanced
2:00remaining
Which code snippet correctly implements multiple interfaces?
Select the code snippet that correctly implements two interfaces in one class.
A
class Multi implements (A, B) {
    public void methodA() {}
    public void methodB() {}
}
B
class Multi implements A & B {
    public void methodA() {}
    public void methodB() {}
}
C
class Multi implements A, B {
    public void methodA() {}
    public void methodB() {}
}
D
class Multi implements A; B {
    public void methodA() {}
    public void methodB() {}
}
Attempts:
2 left
πŸ’‘ Hint
Remember the syntax for implementing multiple interfaces uses commas.
πŸš€ Application
expert
3:00remaining
Determine the output of interface default method override
What is the output of this Java program that uses interface default methods and overrides?
Java
interface Printer {
    default void print() {
        System.out.println("Printing from Printer");
    }
}

interface Scanner {
    default void print() {
        System.out.println("Printing from Scanner");
    }
}

class MultiFunctionMachine implements Printer, Scanner {
    public void print() {
        Printer.super.print();
        Scanner.super.print();
        System.out.println("Printing from MultiFunctionMachine");
    }
}

public class Main {
    public static void main(String[] args) {
        MultiFunctionMachine m = new MultiFunctionMachine();
        m.print();
    }
}
ACompilation error: print() method conflict
BPrinting from MultiFunctionMachine
C
Printing from Printer
Printing from MultiFunctionMachine
D
Printing from Printer
Printing from Scanner
Printing from MultiFunctionMachine
Attempts:
2 left
πŸ’‘ Hint
Look at how the class calls the default methods from both interfaces explicitly.