This set of Java Questions and Answers focuses 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
View Answer
Explanation: 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
View Answer
Explanation: Concrete class implements an interface. They can be instantiated.
3. All methods must be implemented of an interface.
a) True
b) False
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: No, Constructor, Static Initialization Block, Instance Initialization Block and variables cannot be abstract.
Sanfoundry Global Education & Learning Series – Java Programming Language.
To practice all areas of Java, here is complete set on 1000+ Multiple Choice Questions and Answers on Java.
- Practice Programming MCQs
- Practice Information Technology MCQs
- Check Java Books
- Apply for Java Internship
- Practice BCA MCQs