Interfaces & Abstract Classes Questions with Answers

23 practice questions on Interfaces & Abstract Classes (Core Java) with answers and step-by-step solutions. Try 10 samples below, then practise all 23 free in the AptiNest app.

Sample questions

Question 1Easy

Which statement about abstract classes in Java is correct?

  1. An abstract class can have constructors, fields and concrete methods, but cannot be instantiated directly
  2. All methods of an abstract class are implicitly public
  3. An abstract class must contain at least one abstract method
  4. An abstract class cannot have a constructor

Answer: A. An abstract class can have constructors, fields and concrete methods, but cannot be instantiated directly

Explanation

An abstract class may have zero abstract methods and still be abstract. Its constructors run when a subclass is instantiated.

Step-by-step solution

  1. Abstract class can have constructors, fields and concrete methods.
  2. It cannot be instantiated directly.
  3. Abstract class may have zero abstract methods.
  4. Constructors run when subclass is instantiated.
  5. Final answer: An abstract class can have constructors, fields and concrete methods, but cannot be instantiated directly.

Why the other options are wrong

  • B. Methods in an abstract class are not implicitly public; access is set by the modifiers written on each method.
  • C. An abstract class may have zero abstract methods, so at least one abstract method is not required.
  • D. An abstract class can have constructors; they run when a concrete subclass is created.

Question 2Easy

How many interfaces can a single Java class implement?

  1. Any number
  2. Only one
  3. At most two
  4. Only one, plus one abstract class

Answer: A. Any number

Explanation

A class can extend one class but implement any number of interfaces, separated by commas.

Step-by-step solution

  1. Java allows a class to implement multiple interfaces using commas.
  2. A class can implement as many interfaces as needed.
  3. This is different from inheritance where only one superclass is allowed.
  4. So, a Java class can implement any number of interfaces.
  5. The correct answer is Any number.

Why the other options are wrong

  • B. A class can implement several interfaces, so the answer only one is wrong.
  • C. There is no limit of two for interfaces; a class can implement as many as it needs.
  • D. A class can implement many interfaces alongside one superclass, so this answer is not correct.

Tip: Remember: interfaces are like contracts, and a class can fulfill many contracts at once.

Question 3Easy

What happens when this code is compiled?

interface Printer {
    void print(String s);
}

class Console implements Printer {
    void print(String s) { System.out.println(s); }
}

public class Main {
    public static void main(String[] args) {
        new Console().print("hi");
    }
}
  1. Compilation error: print must be public in Console
  2. It compiles; the method is package-private
  3. AbstractMethodError at runtime
  4. hi

Answer: A. Compilation error: print must be public in Console

Explanation

Interface methods are implicitly public, and an implementation cannot reduce visibility, so the implementing method must be declared public.

Step-by-step solution

  1. Interface methods are implicitly public.
  2. Console implements print with no access modifier, so it is package-private.
  3. An implementing method cannot reduce the visibility the interface gives it.
  4. So the code does not compile with a visibility error; option A is the answer.

Why the other options are wrong

  • B. The code does not compile, because the access is reduced from public to package-private.
  • C. No runtime error occurs, because compilation fails first.
  • D. The program never runs, because it does not compile.

Tip: An implementing method must be declared public when it implements an interface method.

Question 4Easy

Which annotation marks an interface intended to have exactly one abstract method?

  1. @Override
  2. @SingleMethod
  3. @FunctionalInterface
  4. @Lambda

Answer: C. @FunctionalInterface

Explanation

@FunctionalInterface makes the compiler check that the interface has exactly one abstract method, so it can be a lambda target.

Step-by-step solution

  1. The annotation for single-abstract-method interfaces is @FunctionalInterface.

Why the other options are wrong

  • A. @Override marks overriding methods.
  • B. There is no such annotation.
  • D. There is no such annotation.

Tip: @FunctionalInterface.

Question 5Medium

What happens when this code is compiled?

interface A {
    default String hi() { return "A"; }
}

interface B {
    default String hi() { return "B"; }
}

class C implements A, B { }

public class Main {
    public static void main(String[] args) {
        System.out.println(new C().hi());
    }
}
  1. B
  2. AB
  3. Compilation error: C inherits unrelated defaults for hi()
  4. A

Answer: C. Compilation error: C inherits unrelated defaults for hi()

Explanation

Two unrelated default methods with the same signature conflict, and C does not override hi() to resolve it, so javac rejects the class.

Step-by-step solution

  1. Class C implements both interfaces A and B which have same method hi().
  2. Both methods return different values but have same signature causing conflict.
  3. Java cannot resolve which method to use in class C without explicit override.
  4. Compiler throws error because of conflicting default methods in interfaces.
  5. Code fails to compile due to unresolved method conflict.

Why the other options are wrong

  • A. Class C gets two defaults, so the call cannot resolve to B alone, and the code does not compile.
  • B. Java does not concatenate default method results, and the conflict makes the code fail to compile.
  • D. A is one of the two conflicting defaults, so Java cannot pick it without an override in C.

Tip: Always override conflicting default methods when implementing multiple interfaces with same method signature.

Question 6Medium

What is the output?

interface Greeter {
    default String greet() { return "Hello " + name(); }
    String name();
}

public class Main {
    public static void main(String[] args) {
        Greeter g = () -> "Asha";
        System.out.println(g.greet());
    }
}
  1. Asha
  2. Hello Asha
  3. Hello null
  4. Compilation error

Answer: B. Hello Asha

Explanation

Greeter has exactly one abstract method, so it is a functional interface and a lambda can implement name(). The default method greet() calls it.

Step-by-step solution

  1. Lambda implements name() to return 'Asha'.
  2. greet() calls name() which returns 'Asha'.
  3. So greet() returns 'Hello Asha'.
  4. System.out.println prints 'Hello Asha'.

Why the other options are wrong

  • A. greet adds Hello before the name, so the output is Hello Asha, not Asha alone.
  • C. name() is implemented by the lambda to return Asha, so the output is not Hello null.
  • D. The code compiles, since Greeter has one abstract method and a default method, so the lambda is valid.

Tip: Remember that default methods can use abstract methods from the same interface.

Question 7Medium

What is the output?

interface Op {
    int apply(int a, int b);
}

public class Main {
    public static void main(String[] args) {
        Op add = new Op() {
            public int apply(int a, int b) { return a + b; }
        };
        Op mul = (a, b) -> a * b;
        System.out.println(add.apply(3, 4) + mul.apply(3, 4));
    }
}
  1. 19
  2. 712
  3. Compilation error
  4. 7

Answer: A. 19

Explanation

The anonymous class returns 7 and the lambda returns 12. Both are int, so + adds them to 19.

Step-by-step solution

  1. add.apply(3, 4) = 3 + 4 = 7.
  2. mul.apply(3, 4) = 3 * 4 = 12.
  3. 7 + 12 = 19.
  4. Final answer: 19.

Why the other options are wrong

  • B. The two results are ints added together, so they give 19, not the text 712.
  • C. The code compiles, since an anonymous class and a lambda both implement Op correctly.
  • D. 7 is only the add result; the mul result of 12 is added as well, giving 19.

Tip: Add results of lambda and interface method directly.

Question 8Medium

Which of these can be declared inside an abstract class but NOT inside an interface?

  1. A default-like method with a body
  2. A protected instance field
  3. A constant (static final field)
  4. A static method

Answer: B. A protected instance field

Explanation

Interfaces cannot have instance fields at all; all their fields are public static final. Abstract classes can have instance fields with any access level.

Step-by-step solution

  1. Interfaces cannot have instance fields; all their fields are public static final.
  2. Abstract classes can have instance fields with any access level, including protected.
  3. So a protected instance field can be declared only in an abstract class.
  4. Option B is the answer.

Why the other options are wrong

  • A. Interfaces can have default methods with bodies, so this is allowed in both.
  • C. Interfaces can have constants, which are static final fields.
  • D. Interfaces can have static methods, so this is allowed in both.

Tip: Interfaces hold behaviour and constants; only classes can hold instance state.

Question 9Hard

What is the output?

interface Shape {
    double area();
    default String describe() { return getClass().getSimpleName() + ":" + (int) area(); }
}

record Square(double side) implements Shape {
    public double area() { return side * side; }
}

public class Main {
    public static void main(String[] args) {
        Shape s = new Square(2.5);
        System.out.println(s.describe());
    }
}
  1. Square:6.25
  2. Shape:6
  3. Square:6
  4. Compilation error

Answer: C. Square:6

Explanation

getClass() returns the runtime class Square. area() is 6.25, and the cast to int truncates it to 6.

Step-by-step solution

  1. getClass() on a Square record returns the runtime class Square, so getSimpleName() is 'Square'.
  2. area() is side × side = 2.5 × 2.5 = 6.25.
  3. The cast (int) truncates 6.25 to 6.
  4. describe() gives Square:6, which is option C.

Why the other options are wrong

  • A. The cast to int removes the decimal part, so the result is 6, not 6.25.
  • B. getClass() returns the runtime class Square, not the interface Shape.
  • D. The code compiles; the record implements area() and so satisfies Shape.

Tip: A cast to int truncates towards zero; it does not round.

Question 10Hard

What is the output?

interface Named {
    default String name() { return "interface"; }
}

class Base {
    public String name() { return "class"; }
}

class Impl extends Base implements Named { }

public class Main {
    public static void main(String[] args) {
        System.out.println(new Impl().name());
    }
}
  1. class
  2. classinterface
  3. interface
  4. Compilation error

Answer: A. class

Explanation

"Class wins": a concrete method inherited from a superclass takes priority over an interface default method with the same signature.

Step-by-step solution

  1. Impl inherits name() from Base, which returns 'class'.
  2. Named's default name() is overridden by Base's implementation.
  3. Method resolution prefers the concrete method from the superclass.
  4. Thus, new Impl().name() calls Base's name() method.
  5. Base's name() returns 'class', so output is 'class'.

Why the other options are wrong

  • B. The output is only class, since no text is joined, and the superclass method wins over the default.
  • C. The interface default is overridden by Base's name(), so interface is not printed.
  • D. The code compiles, since the class method from Base wins over the interface default.

Tip: Concrete class methods always override interface defaults in case of conflict.

Practise all 23 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Core Java topics · All topics