Inheritance and Polymorphism – Multiple Choice Questions (MCQs)
-
-
14. What is the output of the following code? `class Shape { void draw() { System.out.println(\Drawing a shape\"); } } class Circle extends Shape { void draw() { System.out.println(\""Drawing a circle\""); } public static void main(String[] args) { Shape s = new Circle(); s.draw(); } }`"""
-
15. What is the output of the following code? `class Animal { void speak() { System.out.println(\Animal speaks\"); } } class Dog extends Animal { void speak() { System.out.println(\""Dog barks\""); } class Cat extends Animal { void speak() { System.out.println(\""Cat meows\""); } public static void main(String[] args) { Animal animal1 = new Dog(); Animal animal2 = new Cat(); animal1.speak(); animal2.speak(); } }`"""
-
16. What is the purpose of the `final` keyword when applied to a method in the context of inheritance?
-
17. What is the purpose of the `final` keyword when applied to a class in the context of inheritance?
-
18. What is an abstract class in Java?
-
19. Which keyword is used to declare an abstract class in Java?
-
20. Can an abstract class have concrete methods?
-
21. What is an abstract method in Java?
-
22. Which keyword is used to declare an abstract method in Java?
-
23. If a subclass inherits from an abstract class, what must it do with the abstract methods of the superclass?
-
24. What is an interface in Java?