Java Questions & Answers – Interfaces – 2

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

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

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

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

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

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

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

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

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

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

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

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

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

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

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.