AllInfoHub Logo

AllInfoHub – MCQ Practice

Inheritance and Polymorphism – Multiple Choice Questions (MCQs)

  1. 13. What is run-time polymorphism (dynamic binding) in Java?

    • A. Polymorphism achieved through method overloading
    • B. Polymorphism achieved through method overriding
    • C. Polymorphism achieved through inheritance
    • D. Polymorphism achieved through interfaces
  2. 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(); } }`"""

    • A. Drawing a shape
    • B. Drawing a circle
    • C. Error
    • D. None
  3. 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(); } }`"""

    • A. Animal speaks\nAnimal speaks
    • B. Dog barks\nCat meows
    • C. Animal speaks\nCat meows
    • D. Dog barks\nAnimal speaks
  4. 16. What is the purpose of the `final` keyword when applied to a method in the context of inheritance?

    • A. To prevent the method from being called
    • B. To make the method static
    • C. To prevent the method from being overridden in a subclass
    • D. To make the method abstract
  5. 17. What is the purpose of the `final` keyword when applied to a class in the context of inheritance?

    • A. To prevent the class from being instantiated
    • B. To make all methods in the class final
    • C. To prevent the class from being subclassed
    • D. To make all variables in the class final
  6. 18. What is an abstract class in Java?

    • A. A class that cannot be instantiated
    • B. A class with only concrete methods
    • C. A class that must be subclassed
    • D. Both A and C
  7. 19. Which keyword is used to declare an abstract class in Java?

    • A. abstract
    • B. abs
    • C. virtual
    • D. interface
  8. 20. Can an abstract class have concrete methods?

    • A. Yes
    • B. No
    • C. Only static methods
    • D. Only final methods
  9. 21. What is an abstract method in Java?

    • A. A method that has an implementation
    • B. A method declared in an abstract class but without an implementation
    • C. A static method
    • D. A final method
  10. 22. Which keyword is used to declare an abstract method in Java?

    • A. abstract
    • B. abs
    • C. virtual
    • D. interface
  11. 23. If a subclass inherits from an abstract class, what must it do with the abstract methods of the superclass?

    • A. It must declare them as abstract again
    • B. It must provide a concrete implementation for them
    • C. It cannot access them
    • D. It can optionally implement them
  12. 24. What is an interface in Java?

    • A. A class with only abstract methods and constants
    • B. A blueprint for a class that specifies a set of methods that a class must implement
    • C. A way to achieve multiple inheritance
    • D. All of the above