Challenge - 5 Problems
Interface Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediate2: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(); } }
Attempts:
2 left
π‘ Hint
Check if the method speak() is correctly implemented with the right access modifier.
β Incorrect
The class Person implements the interface Speaker and correctly defines the speak() method as public. The program prints the message from the speak() method.
β Predict Output
intermediate2: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(); } }
Attempts:
2 left
π‘ Hint
Look at how the interface reference is assigned to different objects.
β Incorrect
The interface reference 'a' points first to a Dog object, then to a Cat object. Calling sound() prints the respective sounds.
π§ Debug
advanced2: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; } }
Attempts:
2 left
π‘ Hint
Check the access modifier of the implemented method compared to the interface method.
β Incorrect
Interface methods are implicitly public. The implementing method must also be public, otherwise it causes a compile error.
π Syntax
advanced2:00remaining
Which code snippet correctly implements multiple interfaces?
Select the code snippet that correctly implements two interfaces in one class.
Attempts:
2 left
π‘ Hint
Remember the syntax for implementing multiple interfaces uses commas.
β Incorrect
In Java, multiple interfaces are implemented by separating their names with commas, not with & or parentheses or semicolons.
π Application
expert3: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(); } }
Attempts:
2 left
π‘ Hint
Look at how the class calls the default methods from both interfaces explicitly.
β Incorrect
The class overrides print() and calls both interface default methods using InterfaceName.super.method(), then prints its own message.