What is inheritance in Java?
A. The process of acquiring properties and behaviors of one class by another
B. The process of creating objects
C. The process of encapsulation
D. The process of overloading methods
Select an option to see the answer and solution.
In Java, which keyword is used to implement inheritance between classes?
A. inheritsFrom
B. inherits
C. implements
D. extends
Select an option to see the answer and solution.
What is a superclass in Java?
A. The class that inherits properties and behaviors
B. The child class
C. The class that is inherited from
D. The class that is marked as "final"
Select an option to see the answer and solution.
In Java, can a subclass inherit constructors from its superclass?
A. Only if the subclass is marked as "final"
B. Yes, a subclass inherits constructors from its superclass
C. Only if the superclass is marked as "static"
D. None of the above
Select an option to see the answer and solution.
What is method overriding in Java?
A. Making a method static
B. Hiding a superclass method
C. Redefining a superclass method in a subclass to provide a specific implementation
D. Creating a new method with the same name in a subclass
Select an option to see the answer and solution.
In Java, can a subclass override a private method from its superclass?
A. Yes, as long as it's in the same package
B. Yes, regardless of the access modifier
C. No, private methods cannot be overridden
D. No, private methods are not visible to subclasses
Select an option to see the answer and solution.
What is the "super" keyword used for in Java?
A. To call a method or constructor of the superclass
B. To create a new instance of a class
C. To hide a superclass method
D. To make a method final
Select an option to see the answer and solution.
What is the result of the following code snippet?class Parent { void display() { System.out.println("Parent"); } } class Child extends Parent { void display() { System.out.println("Child"); } } public class Main { public static void main(String[] args) { Parent obj = new Child(); obj.display(); } }
A. Runtime exception
B. Compilation error
C. Parent
D. Child
Select an option to see the answer and solution.
In Java, can a subclass access private members (fields and methods) of its superclass?
A. Yes, as long as the subclass is in the same package
B. Yes, regardless of the access modifier
C. No, private members are not visible to subclasses
D. Only if the superclass is marked as "final"
Select an option to see the answer and solution.
What is the difference between "extends" and "implements" in Java?
A. Both are used for class inheritance
B. "Extends" is used for class inheritance, while "implements" is used for interface implementation
C. Both are used for interface implementation
D. None of These
Select an option to see the answer and solution.
In Java, can a subclass access protected members (fields and methods) of its superclass?
A. Only if the subclass is marked as "final"
B. No, protected members are not accessible to subclasses
C. Yes, as long as it's in the same package
D. Yes, regardless of the package
Select an option to see the answer and solution.
What is the result of the following code snippet?class Parent { protected void display() { System.out.println("Parent"); } } class Child extends Parent { void show() { display(); } } public class Main { public static void main(String[] args) { Child obj = new Child(); obj.show(); } }
A. Compilation error
B. Runtime exception
C. Child
D. Parent
Select an option to see the answer and solution.
In Java, can a subclass have more than one superclass?
A. No, a subclass can only have one superclass
B. Yes, a subclass can have multiple superclasses
C. Only if the superclasses have the same name
D. Only if the subclass is marked as "final"
Select an option to see the answer and solution.
What is the purpose of the "final" keyword in Java inheritance?
A. To hide a superclass method
B. To indicate that a class can be inherited only once
C. To make a method non-abstract
D. To indicate that a class cannot be further extended
Select an option to see the answer and solution.
In Java, can a subclass inherit private constructors from its superclass?
A. Yes, constructors are inherited like other methods
B. Only if the subclass is marked as "final"
C. No, constructors are not inherited, especially private constructors
D. Only if the superclass is marked as "static"
Select an option to see the answer and solution.
What is the concept of method overriding in Java inheritance?
A. Making a method static
B. Redefining a superclass method in a subclass with the same method signature
C. Hiding a superclass method
D. None of These
Select an option to see the answer and solution.
In Java, can a subclass inherit static methods from its superclass?
A. Only if the superclass is marked as "abstract"
B. Only if the subclass is marked as "final"
C. Yes, a subclass inherits static methods from its superclass
D. No, static methods are not inherited
Select an option to see the answer and solution.
What is the result of the following code snippet?class Parent { void display() { System.out.println("Parent"); } } class Child extends Parent { void display() { super.display(); System.out.println("Child"); } } public class Main { public static void main(String[] args) { Child obj = new Child(); obj.display(); } }
A. Child Parent
B. Compilation error
C. Runtime exception
D. Parent Child
Select an option to see the answer and solution.
In Java, can a subclass access package-private members (fields and methods) of its superclass defined in another package?
A. No, package-private members are not accessible to subclasses from different packages
B. Yes, as long as it's in the same package
C. Yes, regardless of the package
D. Only if the subclass is marked as "final"
Select an option to see the answer and solution.
What is the result of the following code snippet?class Parent { int x = 10; } class Child extends Parent { int x = 20; void display() { System.out.println(super.x + " " + x); } } public class Main { public static void main(String[] args) { Child obj = new Child(); obj.display(); } }
A. Runtime exception
B. Compilation error
C. 20 10
D. 10 20
Select an option to see the answer and solution.