MCQ Java Interfaces

(Last Updated On: March 13, 2019)

Q) Which keyword is used to declare an interface in java?

  1. class
  2. interface
  3. implements
  4. abstract

 

View Answer
Answer: B
Interface keyword is used to create an interface in java programs. For example,
Interface A{
}

Recommended to read complete notes on interface in java with examples.


 

Q) A java interface can contain ————

  1. public static Final Variables only
  2. public Abstract methods
  3. Abstract methods(unimplemented) and implemented methods both
  4. public static Final Variables and abstract methods both
View Answer
Answer: D
An interface can have both final variables and abstract methods.

 

Q) Which is the correct way to inherit and implement the interface?
Consider and example, Interface is IAnimal and a class is Cat that wants to implement interface.

  1. class Cat implements IAnimal{}
  2. class Cat extends IAnimal{}
  3. class Cat import IAnimal{}
  4. None is correct
View Answer
Answer: A

Classes always implements an interface. An interface can extends another interface or multiple interfaces. Hence, answer would be A.


 

Q) which of the following is true about methods in an interface in java?

  1. An interface can contain only abstract method.
  2. We can define a method in an interface
  3. Private and protected access modifiers can also be used to declare methods in interface
  4. None
View Answer
Answer: A
In java, an interface contains only abstract method that can be public and it does not have any method implementation.

 

Q) Which one is correct declaration for implementing two interfaces?
Consider, Interface A and B.  class C wants to implements both interfaces.

  1. class C implements A, B
  2. class C implements A, implements B
  3. class C implements A extends B
View Answer
Answer: A