Java Interfaces, Abstract Classes & Multiple Inheritance Quiz

Java
0 Passed
0% acceptance

35 in-depth questions (15 with code) covering interface basics, abstract class fundamentals, default/static methods in interfaces, functional interfaces, and multiple inheritance resolution in Java.

35 Questions
~70 minutes
1

Question 1

What is the primary difference between an interface and an abstract class?

A
Interfaces provide pure abstraction with no implementation, abstract classes can have both abstract and concrete methods
B
Abstract classes can be instantiated, interfaces cannot
C
Interfaces support multiple inheritance, abstract classes do not
D
They are identical in functionality
2

Question 2

Can an interface have instance variables?

A
No, interfaces can only have public static final constants (implicitly)
B
Yes, but they must be private
C
Yes, like any class
D
Only in Java 8+
3

Question 3

What is an abstract class?

A
A class that cannot be instantiated and may contain abstract methods that subclasses must implement
B
A class with only static methods
C
An interface with implementation
D
A final class
4

Question 4

Can an abstract class have constructors?

A
Yes, constructors are called when subclasses are instantiated
B
No, abstract classes cannot have constructors
C
Only private constructors
D
Only in Java 8+
5

Question 5

What is a default method in an interface?

A
A method with implementation that classes can inherit or override, added in Java 8
B
A method that is automatically abstract
C
A method that cannot be overridden
D
A static method
6

Question 6

What is a functional interface?

A
An interface with exactly one abstract method, enabling lambda expressions
B
An interface with only default methods
C
An abstract class with one method
D
Any interface
7

Question 7

How does Java resolve the diamond problem in multiple inheritance?

A
Through explicit method overriding in implementing classes
B
By allowing only one implementation path
C
By prohibiting multiple inheritance
D
Automatically choosing the first interface
8

Question 8

Can an interface extend multiple interfaces?

A
Yes, interfaces support multiple inheritance
B
No, interfaces can only extend one interface
C
Only in Java 8+
D
Only with default methods
9

Question 9

What is the output of this interface implementation example?

java
interface Animal {
    void makeSound();
}
class Dog implements Animal {
    public void makeSound() { System.out.println("Woof"); }
}
public class Test {
    public static void main(String[] args) {
        Animal a = new Dog();
        a.makeSound();
    }
}
A
Woof
B
Compilation error
C
Runtime exception
D
No output
10

Question 10

What happens when a class implements multiple interfaces with the same method signature?

A
The class provides one implementation that satisfies all interfaces
B
Compilation error due to ambiguity
C
The first interface's method is used
D
Runtime exception
11

Question 11

Can an abstract class implement an interface?

A
Yes, and it can choose to implement some or all methods
B
No, abstract classes cannot implement interfaces
C
Only if the interface has default methods
D
Only in Java 8+
12

Question 12

What is a static method in an interface?

A
A utility method that belongs to the interface, not instances, added in Java 8
B
A method that cannot be overridden
C
An abstract method
D
A default method
13

Question 13

What is the result of this abstract class example?

java
abstract class Vehicle {
    abstract void start();
    void stop() { System.out.println("Stopping"); }
}
class Car extends Vehicle {
    void start() { System.out.println("Car starting"); }
}
public class Test {
    public static void main(String[] args) {
        Vehicle v = new Car();
        v.start();
        v.stop();
    }
}
A
Car starting Stopping
B
Compilation error
C
Runtime exception
D
Only Car starting
14

Question 14

What is the @FunctionalInterface annotation used for?

A
To ensure an interface has exactly one abstract method and enable lambda compatibility
B
To make an interface abstract
C
To allow multiple inheritance
D
To create default methods
15

Question 15

How do default methods resolve conflicts in multiple inheritance?

A
Implementing classes must override conflicting default methods
B
The first interface's method is chosen automatically
C
Compilation error occurs
D
Default methods cannot conflict
16

Question 16

Can an interface have private methods?

A
Yes, private methods can be used by default methods, added in Java 9
B
No, interfaces cannot have private methods
C
Only static private methods
D
Only in functional interfaces
17

Question 17

What is the output of this multiple interface implementation?

java
interface A { default void show() { System.out.println("A"); } }
interface B { default void show() { System.out.println("B"); } }
class C implements A, B {
    public void show() { System.out.println("C"); }
}
public class Test {
    public static void main(String[] args) {
        new C().show();
    }
}
A
C
B
A
C
B
D
Compilation error
18

Question 18

When should you use an interface instead of an abstract class?

A
When you need multiple inheritance or want to define a contract without implementation
B
When you need to share state between subclasses
C
When you want to prevent inheritance
D
Always, interfaces are better
19

Question 19

What is the result of this functional interface with lambda?

java
interface Calculator {
    int calculate(int a, int b);
}
public class Test {
    public static void main(String[] args) {
        Calculator add = (a, b) -> a + b;
        System.out.println(add.calculate(5, 3));
    }
}
A
8
B
Compilation error
C
Runtime exception
D
0
20

Question 20

Can an abstract class have final methods?

A
Yes, final methods cannot be overridden by subclasses
B
No, abstract classes cannot have final methods
C
Only static final methods
D
Only in Java 8+
21

Question 21

What is the output of this interface with static method?

java
interface MathUtils {
    static int square(int x) { return x * x; }
}
public class Test {
    public static void main(String[] args) {
        System.out.println(MathUtils.square(5));
    }
}
A
25
B
Compilation error
C
Runtime exception
D
0
22

Question 22

How do private methods in interfaces work?

A
They can be called by default methods within the same interface
B
They are accessible to implementing classes
C
They cannot be used in interfaces
D
They are public by default
23

Question 23

What happens when an abstract class implements an interface?

A
It can implement some methods and leave others abstract for subclasses
B
It must implement all methods
C
It cannot implement interfaces
D
Compilation error
24

Question 24

What is the result of this default method inheritance?

java
interface Parent {
    default void show() { System.out.println("Parent"); }
}
class Child implements Parent {
    // No override
}
public class Test {
    public static void main(String[] args) {
        new Child().show();
    }
}
A
Parent
B
Compilation error
C
Runtime exception
D
No output
25

Question 25

Can a class implement multiple interfaces with default methods that conflict?

A
Yes, but the class must override the conflicting method
B
No, compilation error occurs
C
The first interface's method is used
D
Default methods cannot conflict
26

Question 26

What is the purpose of abstract classes in framework design?

A
To provide a template with common functionality that users can extend and customize
B
To prevent any customization
C
To replace interfaces entirely
D
To make frameworks faster
27

Question 27

What is the output of this complex inheritance example?

java
interface A { default void m() { System.out.println("A"); } }
interface B { default void m() { System.out.println("B"); } }
abstract class C implements A, B {
    public void m() { System.out.println("C"); }
}
class D extends C {}
public class Test {
    public static void main(String[] args) {
        new D().m();
    }
}
A
C
B
A
C
B
D
Compilation error
28

Question 28

Why are functional interfaces important for modern Java?

A
They enable lambda expressions and streams, supporting functional programming
B
They replace all other interfaces
C
They make code faster
D
They eliminate the need for classes
29

Question 29

Can an interface extend an abstract class?

A
No, interfaces can only extend other interfaces
B
Yes, but rarely used
C
Only in Java 8+
D
Only with default methods
30

Question 30

What is the result of this interface constant example?

java
interface Constants {
    int MAX = 100;
    String NAME = "Java";
}
public class Test implements Constants {
    public static void main(String[] args) {
        System.out.println(MAX + " " + NAME);
    }
}
A
100 Java
B
Compilation error
C
Runtime exception
D
0 null
31

Question 31

How do default methods affect the single responsibility principle?

A
They allow interfaces to evolve without forcing changes to implementing classes, maintaining SRP
B
They violate SRP by adding behavior to interfaces
C
They eliminate the need for SRP
D
They make interfaces more complex
32

Question 32

What is the output of this abstract class constructor example?

java
abstract class Base {
    Base() { System.out.println("Base constructor"); }
}
class Derived extends Base {
    Derived() { System.out.println("Derived constructor"); }
}
public class Test {
    public static void main(String[] args) {
        new Derived();
    }
}
A
Base constructor Derived constructor
B
Derived constructor Base constructor
C
Base constructor
D
Compilation error
33

Question 33

Why are interfaces preferred for dependency injection?

A
They allow programming to interfaces, enabling easy mocking and testing
B
They provide default implementations
C
They are faster than classes
D
They eliminate dependencies
34

Question 34

What is the result of this functional interface with method reference?

java
interface Processor {
    String process(String s);
}
public class Test {
    public static void main(String[] args) {
        Processor upper = String::toUpperCase;
        System.out.println(upper.process("hello"));
    }
}
A
HELLO
B
hello
C
Compilation error
D
Runtime exception
35

Question 35

Mastering interfaces and abstract classes means you now design code that:

A
Balances abstraction with implementation, uses interfaces for contracts and abstract classes for shared behavior, and resolves multiple inheritance cleanly
B
Avoids inheritance entirely
C
Makes everything an interface
D
Prioritizes speed over design

QUIZZES IN Java