0
0
Javaprogramming~20 mins

Why interfaces are used in Java - Challenge Your Understanding

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!
🧠 Conceptual
intermediate
2:00remaining
Purpose of Interfaces in Java
Why do Java programmers use interfaces in their code?
ATo allow unrelated classes to implement the same set of methods, enabling polymorphism.
BTo store data persistently on disk.
CTo create objects directly without using classes.
DTo increase the speed of program execution by compiling to machine code.
Attempts:
2 left
πŸ’‘ Hint
Think about how different classes can share behavior without sharing a parent class.
❓ Predict Output
intermediate
2:00remaining
Output of Interface Implementation
What is the output of this Java code?
Java
interface Animal {
    void sound();
}

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

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

public class Test {
    public static void main(String[] args) {
        Animal a = new Dog();
        a.sound();
        a = new Cat();
        a.sound();
    }
}
A
Bark
Bark
B
Meow
Bark
CCompilation error due to missing method implementation
D
Bark
Meow
Attempts:
2 left
πŸ’‘ Hint
Look at which class instance is assigned to the interface variable before calling sound().
πŸ”§ Debug
advanced
2:00remaining
Why does this interface code cause a compilation error?
This code tries to implement an interface but causes a compilation error. What is the reason?
Java
interface Vehicle {
    void start();
    void stop();
}

class Car implements Vehicle {
    public void start() {
        System.out.println("Car started");
    }
    // Missing stop() method implementation
}

public class Test {
    public static void main(String[] args) {
        Vehicle v = new Car();
        v.start();
        v.stop();
    }
}
AInterfaces cannot be implemented by classes.
BThe class Car does not implement all methods declared in the Vehicle interface.
CThe main method is missing the throws declaration.
DThe interface Vehicle cannot have more than one method.
Attempts:
2 left
πŸ’‘ Hint
Check if Car has all methods that Vehicle requires.
πŸ“ Syntax
advanced
2:00remaining
Identify the Syntax Error in Interface Declaration
Which option contains a syntax error in the interface declaration?
A
interface Shape {
    double area() {}
}
B
interface Shape {
    double area();
}
C
interface Shape {
    double area();
    void draw();
}
D
interface Shape {
    void resize(int size);
}
Attempts:
2 left
πŸ’‘ Hint
Interfaces cannot have method bodies unless they are default or static methods.
πŸš€ Application
expert
3:00remaining
Using Interfaces for Multiple Inheritance
Given these interfaces and classes, what will be the output when running the main method?
Java
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();
    }
}
A
Showing document
Printing document
BCompilation error due to multiple inheritance
C
Printing document
Showing document
DRuntime error due to method conflict
Attempts:
2 left
πŸ’‘ Hint
Interfaces allow a class to inherit from multiple sources without conflict if methods are implemented.