Java Polymorphism
Master compile-time and runtime polymorphism, method overloading, dynamic dispatch, and casting in Java.
Questions in this quiz
20 questions on Java Polymorphism. Work through them here, then start the quiz to answer them and see your score.
1.What does "polymorphism" mean in the context of Java?
- AA class can only have one form
- BTwo classes must share the same code
- CVariables can change their data type at runtime
- DAn object or method can take many forms
2.What is compile-time polymorphism in Java?
- ADynamic dispatch
- BInterface implementation
- CMethod overloading
- DMethod overriding
3.What is runtime polymorphism in Java?
- AMethod overloading resolved at compile time
- BCasting a type at runtime
- CDynamic method dispatch through method overriding
- DUsing generic types
4.What does this code print?
class Printer { void print(int n) { System.out.println("int: " + n); } void print(double n) { System.out.println("double: " + n); } void print(String s) { System.out.println("String: " + s); } } public class Main { public static void main(String[] args) { new Printer().print(3.14); } }- Adouble: 3.14
- Bint: 3
- CCompilation error
- DString: 3.14
5.Which of the following is an example of runtime polymorphism?
- ACalling an overloaded method with an int argument
- BUsing the final keyword on a method
- CCalling a static method through a class name
- DA parent reference variable pointing to a child object and calling an overridden method
6.What does this code print?
class Calculator { int add(int a, int b) { return a + b; } int add(int a, int b, int c) { return a + b + c; } double add(double a, double b) { return a + b; } } public class Main { public static void main(String[] args) { Calculator c = new Calculator(); System.out.println(c.add(1, 2)); } }- A1.0
- B3.0
- CCompilation error
- D3
7.What is upcasting in Java?
- ACasting a parent reference to a child type
- BConverting an int to a long
- CCalling a method higher up the hierarchy
- DCasting a child reference to a parent type
8.What is downcasting in Java?
- ACalling super methods
- BExplicitly casting a parent reference back to a child type
- CAutomatically converting a child to a parent type
- DReducing a long to an int
9.What does this code print?
class Animal { void speak() { System.out.println("..."); } } class Cat extends Animal { void speak() { System.out.println("Meow"); } } class Dog extends Animal { void speak() { System.out.println("Woof"); } } public class Main { public static void main(String[] args) { Animal[] animals = { new Cat(), new Dog(), new Cat() }; for (Animal a : animals) a.speak(); } }- A... ... ...
- BMeow Woof Meow
- CMeow Woof Meow
- DCompilation error
10.What is dynamic dispatch?
- AResolving method calls at compile time
- BThe JVM selecting the correct overridden method at runtime based on the object type
- CLoading classes dynamically with ClassLoader
- DDispatching events to listeners
11.What exception is thrown when you incorrectly downcast?
- AClassCastException
- BCastingException
- CNullPointerException
- DIllegalStateException
12.What does this code output?
class Base { void show() { System.out.println("Base"); } static void display() { System.out.println("Static Base"); } } class Derived extends Base { void show() { System.out.println("Derived"); } static void display() { System.out.println("Static Derived"); } } public class Main { public static void main(String[] args) { Base obj = new Derived(); obj.show(); obj.display(); } }- ABase Static Base
- BDerived Static Derived
- CBase Static Derived
- DDerived Static Base
13.Which of the following correctly uses instanceof before downcasting?
Animal a = new Dog(); if (a instanceof Dog) { Dog d = (Dog) a; d.fetch(); }- AThis code will throw ClassCastException
- BYou cannot cast Animal to Dog
- CThis is the correct safe downcasting pattern
- Dinstanceof is not needed here
14.Can you overload a method by changing only the return type?
- AOnly if the parameters also differ
- BYes, but only for primitive types
- CYes, different return types create different overloads
- DNo, overloading requires different parameter lists
15.What is the output of this code?
class Parent { String name = "Parent"; String getName() { return "Parent method"; } } class Child extends Parent { String name = "Child"; String getName() { return "Child method"; } } public class Main { public static void main(String[] args) { Parent p = new Child(); System.out.println(p.name); System.out.println(p.getName()); } }- AChild Parent method
- BParent Child method
- CParent Parent method
- DChild Child method
16.What is ad-hoc polymorphism?
- APolymorphism through inheritance
- BInterface-based polymorphism
- CUsing generic type parameters
- DPolymorphism achieved through method overloading or operator overloading
17.What is parametric polymorphism in Java?
- AMethod overloading with different parameter counts
- BUsing abstract parameters in abstract methods
- CPassing parameters to constructors
- DGenerics — writing code that works for any type using type parameters
18.What does this code print?
class A { int value; A(int v) { this.value = v; } void show() { System.out.println("A: " + value); } } class B extends A { B(int v) { super(v * 2); } void show() { System.out.println("B: " + value); } } public class Main { public static void main(String[] args) { A obj = new B(5); obj.show(); } }- AA: 10
- BB: 10
- CB: 5
- DA: 5
19.Which of the following is NOT a benefit of polymorphism?
- ACode reuse through a common interface
- BFlexibility to add new types without changing existing code
- CGuaranteed better performance than non-polymorphic code
- DSubstitutability — child objects can replace parent objects
20.What is the Liskov Substitution Principle related to polymorphism?
- AEvery class must implement at least one interface
- BSubclasses must override all parent methods
- CStatic methods can replace instance methods
- DObjects of a parent type should be replaceable with objects of child types without breaking the program