AllInfoHub Logo

AllInfoHub – MCQ Practice

Inheritance and Polymorphism – Multiple Choice Questions (MCQs)

  1. 25. Which keyword is used to declare an interface in Java?

    • A. interface
    • B. implements
    • C. abstract class
    • D. protocol
  2. 26. Which keyword is used by a class to implement an interface?

    • A. extends
    • B. inherits
    • C. implements
    • D. subclass
  3. 27. Can a class implement multiple interfaces in Java?

    • A. Yes
    • B. No
    • C. Only one
    • D. Only if the interfaces have no common methods
  4. 28. Can an interface have concrete methods in Java versions prior to Java 8?

    • A. Yes
    • B. No
    • C. Only static methods
    • D. Only default methods
  5. 29. What are default methods in interfaces (introduced in Java 8)?

    • A. Concrete methods defined within an interface with the `default` keyword
    • B. Abstract methods that have a default implementation in implementing classes
    • C. Static methods in interfaces
    • D. Private methods in interfaces
  6. 30. What are static methods in interfaces (introduced in Java 8)?

    • A. Methods that can be called on the interface itself
    • B. without needing an implementing class instance
    • C. Abstract methods
    • D. Default methods
  7. 31. What is the output of the following code? `interface Printable { void print(); } class Document implements Printable { public void print() { System.out.println(\Printing document\"); } public static void main(String[] args) { Printable p = new Document(); p.print(); } }`"""

    • A. Printable
    • B. Document
    • C. Printing document
    • D. Error
  8. 32. What is the output of the following code? `interface Swimmable { void swim(); } class Fish implements Swimmable { public void swim() { System.out.println(\Fish swims\"); } } class Duck extends Animal implements Swimmable { public void swim() { System.out.println(\""Duck swims\""); } void makeSound() { System.out.println(\""Quack\""); } public static void main(String[] args) { Swimmable s1 = new Fish(); Swimmable s2 = new Duck(); s1.swim(); s2.swim(); } }`"""

    • A. Fish swims\nDuck swims
    • B. Duck swims\nFish swims
    • C. Fish swims\nQuack
    • D. Duck swims\nQuack
  9. 33. What is the purpose of the `instanceof` operator in Java?

    • A. To check if an object is an instance of a particular class or interface
    • B. To compare two objects for equality
    • C. To get the class of an object
    • D. To cast an object to another type
  10. 34. What will be the result of `(new Dog() instanceof Animal)` given the class hierarchy `Animal <- Dog`?

    • A. TRUE
    • B. FALSE
    • C. Error
    • D. Depends on the JVM
  11. 35. What will be the result of `(new Animal() instanceof Dog)` given the class hierarchy `Animal <- Dog`?

    • A. TRUE
    • B. FALSE
    • C. Error
    • D. Depends on the JVM
  12. 36. What is the output of the following code? `class A {} class B extends A {} public class InstanceOfDemo { public static void main(String[] args) { A obj = new B(); System.out.println(obj instanceof A); System.out.println(obj instanceof B); } }`

    • A. true\ntrue
    • B. true\nfalse
    • C. false\ntrue
    • D. false\nfalse