MCQ Java Interfaces

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

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

Answer: 2
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

Answer: 4
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

Answer: 1

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

Answer: 1
In java, an interface contains only abstract method that can be public and it does not have any method implementation. NOTE: From Java8 you can define a default method in an interface.


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

Answer: 1


Related Posts