QuizzesJava Abstraction & Interfaces
intermediate
java

Java Abstraction & Interfaces

Test your knowledge of abstract classes, interfaces, default methods, functional interfaces, and design with abstraction in Java.

25 minutes 20 questions Pass at 70%
Time Limit
25 minutes - timer starts when you begin
Change Answers Freely
Revisit and change any answer before submitting
Full Review After Submit
Correct answers and explanations shown for every question
Auto-submit on timeout
Quiz submits automatically when time runs out

Questions in this quiz

20 questions on Java Abstraction & Interfaces. Work through them here, then start the quiz to answer them and see your score.

  1. 1.What keyword is used to declare an abstract class in Java?

    • Avirtual
    • Babstract
    • Cbase
    • Dinterface
  2. 2.Which statement about abstract methods is correct?

    • AAbstract methods can only exist in interfaces
    • BAbstract methods must have a body
    • CAbstract methods are automatically static
    • DAbstract methods have no body and must be overridden by subclasses
  3. 3.Can an abstract class have a constructor?

    • ANo, abstract classes cannot have constructors
    • BOnly if the class has no abstract methods
    • CYes, but only a no-arg constructor
    • DYes, and it is called when a subclass object is created
  4. 4.What does this code do?

    abstract class Shape {
        abstract double area();
        void describe() {
            System.out.println("Area: " + area());
        }
    }
    class Circle extends Shape {
        double r;
        Circle(double r) { this.r = r; }
        double area() { return Math.PI * r * r; }
    }
    public class Main {
        public static void main(String[] args) {
            new Circle(5).describe();
        }
    }
    • APrints Area: 0.0
    • BPrints Area: 78.53...
    • CRuntime error
    • DCompilation error — cannot call area() in abstract class
  5. 5.What is the key difference between an abstract class and an interface in Java?

    • AAn abstract class can have state and constructors; an interface primarily defines a contract
    • BThere is no difference since Java 8
    • CInterfaces support inheritance; abstract classes do not
    • DAbstract classes cannot have methods; interfaces can
  6. 6.How many interfaces can a Java class implement?

    • AUp to 3
    • BUnlimited
    • CUp to 10
    • DOnly 1
  7. 7.What are default methods in interfaces (introduced in Java 8)?

    • AConcrete methods inside an interface with the default keyword
    • BAbstract methods that have a default implementation in Object
    • CMethods that return default values for primitives
    • DMethods that are automatically called by the JVM
  8. 8.What does this code print?

    interface Greeter {
        default String greet() { return "Hello"; }
    }
    class FormalGreeter implements Greeter {
        public String greet() { return "Good day"; }
    }
    class CasualGreeter implements Greeter {}
    public class Main {
        public static void main(String[] args) {
            System.out.println(new FormalGreeter().greet());
            System.out.println(new CasualGreeter().greet());
        }
    }
    • AGood day Hello
    • BHello Hello
    • CGood day Good day
    • DCompilation error
  9. 9.Can an interface have static methods in Java 8+?

    • ANo, interfaces cannot have any concrete methods
    • BYes, but they are inherited by implementing classes
    • COnly if the interface extends another interface
    • DYes, and they are called on the interface name, not an instance
  10. 10.What is a functional interface?

    • AAn interface with no methods
    • BAn interface that extends Runnable
    • CAn interface with exactly one abstract method
    • DAn interface with only static methods
  11. 11.What does this code print?

    @FunctionalInterface
    interface MathOp {
        int operate(int a, int b);
    }
    public class Main {
        public static void main(String[] args) {
            MathOp add = (a, b) -> a + b;
            MathOp multiply = (a, b) -> a * b;
            System.out.println(add.operate(3, 4));
            System.out.println(multiply.operate(3, 4));
        }
    }
    • ACompilation error
    • B34 34
    • C12 7
    • D7 12
  12. 12.When should you choose an abstract class over an interface?

    • AWhen you want multiple inheritance of type
    • BWhen you want to define a pure contract with no shared state
    • CWhen related classes share common state or non-public methods
    • DWhen you need lambda support
  13. 13.What happens if two interfaces have conflicting default methods and a class implements both?

    • ACompilation error — the class must override the conflicting method
    • BRuntime exception when the method is called
    • CThe second interface's version always wins
    • DThe JVM picks the first interface alphabetically
  14. 14.What does this code print?

    interface A { default void hello() { System.out.println("A"); } }
    interface B { default void hello() { System.out.println("B"); } }
    class C implements A, B {
        public void hello() {
            A.super.hello();
            B.super.hello();
        }
    }
    public class Main {
        public static void main(String[] args) { new C().hello(); }
    }
    • ACompilation error
    • BB
    • CA
    • DA B
  15. 15.Can interfaces have instance fields in Java?

    • AYes, any kind of field
    • BOnly if the interface is abstract
    • COnly public static final (constant) fields
    • DNo, interfaces cannot have any fields
  16. 16.What is the purpose of the @FunctionalInterface annotation?

    • AIt enables the interface to be serialised
    • BIt enforces at compile time that the interface has exactly one abstract method
    • CIt makes the interface execute faster
    • DIt automatically generates lambda support
  17. 17.What does this code print?

    abstract class Vehicle {
        abstract String fuel();
        void describe() {
            System.out.println("I run on " + fuel());
        }
    }
    class ElectricCar extends Vehicle {
        String fuel() { return "electricity"; }
    }
    class PetrolCar extends Vehicle {
        String fuel() { return "petrol"; }
    }
    public class Main {
        public static void main(String[] args) {
            Vehicle v = new ElectricCar();
            v.describe();
        }
    }
    • AI run on petrol
    • BCompilation error
    • CI run on null
    • DI run on electricity
  18. 18.Can you instantiate an interface directly in Java?

    • AYes, if the interface has only default methods
    • BYes, using the new keyword
    • COnly with reflection
    • DNo, but you can create anonymous class instances
  19. 19.What is the Comparable interface used for?

    • ASorting arrays of primitives
    • BDefining a natural ordering for a class by implementing compareTo()
    • CChecking if two objects are equal using equals()
    • DComparing two separate objects externally
  20. 20.Which of the following best describes interface segregation (the "I" in SOLID)?

    • AClients should not be forced to depend on methods they do not use — prefer small focused interfaces
    • BInterfaces should only contain constants
    • CAll interfaces must have exactly one method
    • DClasses should implement as many interfaces as possible