Java Questions & Answers – Inheritance – 2

This set of Java Questions and Answers focuses on “Inheritance – 2”.

1. What is not type of inheritance?
a) Single inheritance
b) Double inheritance
c) Hierarchical inheritance
d) Multiple inheritance
View Answer

Answer: b
Explanation: Inheritance is way of acquiring attributes and methods of parent class. Java supports hierarchical inheritance directly.

2. Using which of the following, multiple inheritance in Java can be implemented?
a) Interfaces
b) Multithreading
c) Protected methods
d) Private methods
View Answer

Answer: a
Explanation: Multiple inheritance in java is implemented using interfaces. Multiple interfaces can be implemented by a class.

3. All classes in Java are inherited from which class?
a) java.lang.class
b) java.class.inherited
c) java.class.object
d) java.lang.Object
View Answer

Answer: d
Explanation: All classes in java are inherited from Object class. Interfaces are not inherited from Object Class.
advertisement
advertisement

4. In order to restrict a variable of a class from inheriting to subclass, how variable should be declared?
a) Protected
b) Private
c) Public
d) Static
View Answer

Answer: b
Explanation: By declaring variable private, the variable will not be available in inherited to subclass.

5. If super class and subclass have same variable name, which keyword should be used to use super class?
a) super
b) this
c) upper
d) classname
View Answer

Answer: a
Explanation: Super keyword is used to access hidden super class variable in subclass.

6. Static members are not inherited to subclass.
a) True
b) False
View Answer

Answer: b
Explanation: Static members are also inherited to subclasses.

7. Which of the following is used for implementing inheritance through an interface?
a) inherited
b) using
c) extends
d) implements
View Answer

Answer: d
Explanation: Interface is implemented using implements keyword. A concrete class must implement all the methods of an interface, else it must be declared abstract.
advertisement

8. Which of the following is used for implementing inheritance through class?
a) inherited
b) using
c) extends
d) implements
View Answer

Answer: c
Explanation: Class can be extended using extends keyword. One class can extend only one class. A final class cannot be extended.

9. What would be the result if a class extends two interfaces and both have a method with same name and signature? Lets assume that the class is not implementing that method.
a) Runtime error
b) Compile time error
c) Code runs successfully
d) First called method is executed successfully
View Answer

Answer: b
Explanation: In case of such conflict, compiler will not be able to link a method call due to ambiguity. It will throw compile time error.
advertisement

10. Does Java support Multilevel inheritance?
a) True
b) False
View Answer

Answer: a
Explanation: Yes, Java supports multilevel inheritance. In Java, a class can inherit from another class, which itself inherits from another class, and so on. Kindly note that this is different than Multiple inheritance which is not supported natively in Java; however, it can be implemented using multiple interfaces.

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.