0
0
Javaprogramming~20 mins

Default methods in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Default Methods Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of default method call in interface
What is the output of this Java code?
Java
interface Speaker {
    default void sayHello() {
        System.out.println("Hello from Speaker");
    }
}

class Person implements Speaker {
}

public class Main {
    public static void main(String[] args) {
        Person p = new Person();
        p.sayHello();
    }
}
ACompilation error: sayHello() not implemented
BNo output
CRuntime error: NullPointerException
DHello from Speaker
Attempts:
2 left
πŸ’‘ Hint
Remember that default methods in interfaces provide an implementation that classes can inherit.
❓ Predict Output
intermediate
2:00remaining
Output when overriding default method
What will be printed when running this code?
Java
interface Printer {
    default void print() {
        System.out.println("Printing from Printer");
    }
}

class Document implements Printer {
    public void print() {
        System.out.println("Printing from Document");
    }
}

public class Main {
    public static void main(String[] args) {
        Document doc = new Document();
        doc.print();
    }
}
APrinting from Printer
BPrinting from Document
CRuntime error: MethodNotFoundException
DCompilation error: print() must be public
Attempts:
2 left
πŸ’‘ Hint
Check if the class overrides the default method.
❓ Predict Output
advanced
2:00remaining
Resolving conflict between two default methods
What is the output of this code?
Java
interface A {
    default void show() {
        System.out.println("A show");
    }
}

interface B {
    default void show() {
        System.out.println("B show");
    }
}

class C implements A, B {
    public void show() {
        A.super.show();
    }
}

public class Main {
    public static void main(String[] args) {
        C c = new C();
        c.show();
    }
}
ACompilation error: class C must override show()
BB show
CA show
DRuntime error: AmbiguousMethodException
Attempts:
2 left
πŸ’‘ Hint
When two interfaces have the same default method, the implementing class must resolve the conflict explicitly.
πŸ”§ Debug
advanced
2:00remaining
Identify the compilation error in default method usage
This code does not compile. What is the cause of the compilation error?
Java
interface X {
    default void doSomething() {
        System.out.println("Doing something");
    }
}

class Y implements X {
    public void doSomething() {
        System.out.println("Doing something else");
    }
}

public class Main {
    public static void main(String[] args) {
        Y y = new Y();
        y.doSomething();
    }
}
AMethod doSomething() in class Y must be public to override interface method
BInterface X cannot have default methods
CClass Y must be abstract
DMain class must implement interface X
Attempts:
2 left
πŸ’‘ Hint
Check the access modifier of the overriding method compared to the interface method.
🧠 Conceptual
expert
2:00remaining
Behavior of default methods with multiple inheritance and diamond problem
Consider interfaces I1 and I2 both have a default method foo(). Class D implements both interfaces but does NOT override foo(). What happens when foo() is called on an instance of D?
ACompilation error due to diamond problem; class D must override foo()
BRuntime error due to ambiguous method call
CThe foo() method from I2 is called by default
DThe foo() method from I1 is called by default
Attempts:
2 left
πŸ’‘ Hint
Java requires explicit resolution when multiple interfaces provide the same default method.