Java Interfaces

Last Updated :
Discuss
Comments

Question 1

Which of the following statements about Java interfaces is true?

  • A

    An interface can have instance variables

  • B

    An interface can extend multiple interfaces

  • C

    An interface can implement another interface

  • D

    An interface can have a constructor

Question 2

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

  • A

    interface MyInterface { void myMethod() {}; }


  • B

    abstract interface MyInterface { void myMethod(); }


  • C

    public interface MyInterface { void myMethod(); }

  • D

    class MyInterface { void myMethod(); }

Question 3

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);
    }
}


  • A

    10

  • B

    Compilation Error

  • C

    Runtime Error

  • D

    NullPointerException


Question 4

Can an interface method be private?

  • A

    Yes, but only in Java 9 and later


  • B

    No, interface methods must always be public

  • C

    Yes, in any Java version

  • D

    Only if the method is static

Question 5

What is the main difference between class and interface?


  • A

    Class can’t have variables

  • B

    Interface can be instantiated

  • C

    Interface can’t have method bodies (pre Java 8)

  • D

    Class has only abstract methods

Question 6

Which Java feature allows multiple inheritance of type?


  • A

    Interface


  • B

    Abstract class


  • C

    Inheritance

  • D

    Static block


Question 7

Can an interface have a method body?

  • A

    No, never

  • B

    No, only abstract methods

  • C

    Yes, using default and static methods (Java 8+)

  • D

    yes, but only private methods

Question 8

How many abstract methods can a functional interface have?


  • A

    0

  • B

    1

  • C

    2

  • D

    Unlimited

Question 9

Which of the following is a valid functional interface in Java?

  • A

    Comparator

  • B

    Runnable


  • C

    Callable

  • D

    All of the above


There are 9 questions to complete.

Take a part in the ongoing discussion