Inheritance and Polymorphism – Multiple Choice Questions (MCQs)
-
-
2. Which keyword is used to implement inheritance in Java?
-
3. What is a superclass (or parent class) in Java?
-
4. What is a subclass (or child class) in Java?
-
5. Can a class inherit from multiple classes directly in Java?
-
6. What is the output of the following code? `class Animal { void makeSound() { System.out.println(\Generic sound\"); } } class Dog extends Animal { void makeSound() { System.out.println(\""Woof\""); } public static void main(String[] args) { Dog myDog = new Dog(); myDog.makeSound(); } }`"""
-
7. What is method overriding in Java?
-
8. How do you call a method of the superclass from within a subclass method that has overridden it?
-
9. What is the output of the following code? `class Parent { void greet() { System.out.println(\Hello from Parent\"); } } class Child extends Parent { void greet() { super.greet(); System.out.println(\""Hello from Child\""); } public static void main(String[] args) { Child myChild = new Child(); myChild.greet(); } }`"""
-
10. What is polymorphism in Java?
-
11. Which of the following is NOT a way to achieve polymorphism in Java?
-
12. What is compile-time polymorphism (static binding) in Java?