Inheritance and Polymorphism – Multiple Choice Questions (MCQs)
-
-
26. Which keyword is used by a class to implement an interface?
-
27. Can a class implement multiple interfaces in Java?
-
28. Can an interface have concrete methods in Java versions prior to Java 8?
-
29. What are default methods in interfaces (introduced in Java 8)?
-
30. What are static methods in interfaces (introduced in Java 8)?
-
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(); } }`"""
-
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(); } }`"""
-
33. What is the purpose of the `instanceof` operator in Java?
-
34. What will be the result of `(new Dog() instanceof Animal)` given the class hierarchy `Animal <- Dog`?
-
35. What will be the result of `(new Animal() instanceof Dog)` given the class hierarchy `Animal <- Dog`?
-
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); } }`