Java Interfaces

Here are 10 essential multiple-choice questions on Java Interfaces, covering key concepts.


Last Updated :
Discuss
Comments

Question 1

Which of the following statements about Java interfaces is true?

  • An interface can have instance variables

  • An interface can extend multiple interfaces

  • An interface can implement another interface

  • An interface can have a constructor

Question 2

What will be the output of the following code?

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() {         A.super.show();     }     public static void main(String[] args) {         new C().show();     } } 
  • A

  • B

  • Compilation Error

  • Runtime Error

Question 3

What is the correct way to declare an interface in Java?

  • interface MyInterface { void myMethod() {}; }


  • abstract interface MyInterface { void myMethod(); }


  • public interface MyInterface { void myMethod(); }

  • class MyInterface { void myMethod(); }

Question 4

Which of the following is true about interfaces and abstract classes?


  • An abstract class can implement multiple interfaces

  • An interface can implement an abstract class

  • An abstract class cannot have final methods


  • An interface cannot have a static method


Question 5

What happens if a class implements two interfaces with the same default method?

  • Compilation fails

  • Runtime error occurs

  • The class must override the method


  • The method from the first interface is used

Question 6

Which of the following is NOT a valid use of Java interfaces?

  • Defining method contracts without implementation

  • Supporting multiple inheritance

  • Achieving polymorphism


  • Extending a class

Question 7

What will be the output of the following code?

Java
interface X {     int VALUE = 10; } class Y implements X {     public static void main(String[] args) {         System.out.println(VALUE);     } } 


  • 10

  • Compilation Error

  • Runtime Error

  • NullPointerException


Question 8

Can an interface method be private?

  • Yes, but only in Java 9 and later


  • No, interface methods must always be public

  • Yes, in any Java version

  • Only if the method is static

Question 9

Which of the following correctly implements an interface with a method?

  • Java
    interface Test { void method() {} } class Demo implements Test {} 


  • Java
    interface Test { void method(); } class Demo implements Test { public void method() {} } 


  • Java
    interface Test { void method(); } class Demo implements Test { void method() {} } 


  • Java
    interface Test { void method(); } class Demo extends Test {} 


Question 10

How does Java differentiate between an interface and an abstract class?

  • An interface cannot have any method implementations, but an abstract class can.

  • Abstract classes support constructors, but interfaces do not.

  • A class can implement multiple interfaces but can extend only one abstract class.

  • All of the above

There are 10 questions to complete.

Take a part in the ongoing discussion