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
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
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
Explanation: All classes in java are inherited from Object class. Interfaces are not inherited from Object Class.
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
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
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
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
Explanation: Interface is implemented using implements keyword. A concrete class must implement all the methods of an interface, else it must be declared abstract.
8. Which of the following is used for implementing inheritance through class?
a) inherited
b) using
c) extends
d) implements
View Answer
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
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.
10. Does Java support Multilevel inheritance?
a) True
b) False
View Answer
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.
- Check Programming Books
- Check Java Books
- Practice Programming MCQs
- Apply for Java Internship
- Apply for Computer Science Internship