250+ TOP MCQs on Interfaces – 2 and Answers

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?
a) Protected
b) Private
c) Public
d) Public, protected, private

Answer: a
Clarification: Interface can have either public access specifier or no specifier. The reason is they need to be implemented by other classes.

2. Which of the following is the correct way of implementing an interface A by class B?
a) class B extends A{}
b) class B implements A{}
c) class B imports A{}
d) None of the mentioned

Answer: b
Clarification: Concrete class implements an interface. They can be instantiated.

3. All methods must be implemented of an interface.
a) True
b) False

Answer: a
Clarification: Concrete classes must implement all methods in an interface. Through interface multiple inheritance is possible.

4. What type of variable can be defined in an interface?
a) public static
b) private final
c) public final
d) static final

Answer: d
Clarification: variable defined in an interface is implicitly final and static. They are usually written in capital letters.

5. What does an interface contain?
a) Method definition
b) Method declaration
c) Method declaration and definition
d) Method name

Answer: b
Clarification: Interface contains the only declaration of the method.

6. What type of methods an interface contain by default?
a) abstract
b) static
c) final
d) private

Answer: a
Clarification: By default, interface contains abstract methods. The abstract methods need to be implemented by concrete classes.

7. What will happen if we provide concrete implementation of method in interface?
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

Answer: c
Clarification: The methods of interfaces are always abstract. They provide only method definition.

8. What happens when a constructor is defined for an interface?
a) Compilation failure
b) Runtime Exception
c) The interface compiles successfully
d) The implementing class will throw exception

Answer: a
Clarification: Constructor is not provided by interface as objects cannot be instantiated.

9. What happens when we access the same variable defined in two interfaces implemented by the same class?
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

Answer: d
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.

10. Can “abstract” keyword be used with constructor, Initialization Block, Instance Initialization and Static Initialization Block.
a) True
b) False

Answer: b
Clarification: No, Constructor, Static Initialization Block, Instance Initialization Block and variables cannot be abstract.

Java for Interviews, here is complete set on Multiple Choice Questions and Answers on Java.

contest

Leave a Reply

Your email address will not be published. Required fields are marked *