This set of Java Interview Questions and Answers for Experienced people on “Interfaces – 2”.
1. Which of the following access specifiers can be used for an interface? Answer: a 2. Which of the following is the correct way of implementing an interface A by class B? Answer: b 3. All methods must be implemented of an interface. Answer: a 4. What type of variable can be defined in an interface? Answer: d 5. What does an interface contain? Answer: b 6. What type of methods an interface contain by default? Answer: a 7. What will happen if we provide concrete implementation of method in interface? Answer: c 8. What happens when a constructor is defined for an interface? Answer: a 9. What happens when we access the same variable defined in two interfaces implemented by the same class? Answer: d 10. Can “abstract” keyword be used with constructor, Initialization Block, Instance Initialization and Static Initialization Block. Answer: b Java for Interviews, here is complete set on Multiple Choice Questions and Answers on Java.
a) Protected
b) Private
c) Public
d) Public, protected, private
Clarification: Interface can have either public access specifier or no specifier. The reason is they need to be implemented by other classes.
a) class B extends A{}
b) class B implements A{}
c) class B imports A{}
d) None of the mentioned
Clarification: Concrete class implements an interface. They can be instantiated.
a) True
b) False
Clarification: Concrete classes must implement all methods in an interface. Through interface multiple inheritance is possible.
a) public static
b) private final
c) public final
d) static final
Clarification: variable defined in an interface is implicitly final and static. They are usually written in capital letters.
a) Method definition
b) Method declaration
c) Method declaration and definition
d) Method name
Clarification: Interface contains the only declaration of the method.
a) abstract
b) static
c) final
d) private
Clarification: By default, interface contains abstract methods. The abstract methods need to be implemented by concrete classes.
a) The concrete class implementing that method need not provide implementation of that method
b) Runtime exception is thrown
c) Compilation failure
d) Method not found exception is thrown
Clarification: The methods of interfaces are always abstract. They provide only method definition.
a) Compilation failure
b) Runtime Exception
c) The interface compiles successfully
d) The implementing class will throw exception
Clarification: Constructor is not provided by interface as objects cannot be instantiated.
a) Compilation failure
b) Runtime Exception
c) The JVM is not able to identify the correct variable
d) The interfaceName.variableName needs to be defined
Clarification: The JVM needs to distinctly know which value of variable it needs to use. To avoid confusion to the JVM interfaceName.variableName is mandatory.
a) True
b) False
Clarification: No, Constructor, Static Initialization Block, Instance Initialization Block and variables cannot be abstract.