Java Polymorphism (Compile-Time & Runtime) Quiz
35 in-depth questions (15 with code) covering method overloading as compile-time polymorphism, overriding as runtime polymorphism, dynamic method dispatch, upcasting/downcasting, and why polymorphism matters in object-oriented design.
Question 1
What is polymorphism in object-oriented programming?
Question 2
What is the difference between compile-time and runtime polymorphism?
Question 3
What is method overloading?
Question 4
What is method overriding?
Question 5
What is dynamic method dispatch?
Question 6
What is upcasting?
Question 7
What is downcasting?
Question 8
Why does polymorphism matter in software design?
Question 9
What is the output of this method overloading example?
class Calculator {
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
}
public class Test {
public static void main(String[] args) {
Calculator c = new Calculator();
System.out.println(c.add(5, 3));
System.out.println(c.add(5.0, 3.0));
}
}Question 10
What is printed by this runtime polymorphism example?
class Animal {
void sound() { System.out.println("Animal sound"); }
}
class Dog extends Animal {
void sound() { System.out.println("Woof"); }
}
public class Test {
public static void main(String[] args) {
Animal a = new Dog();
a.sound();
}
}Question 11
Can overloaded methods have different return types?
Question 12
What happens when you try to override a method with a different return type?
Question 13
What is the result of this upcasting example?
class Vehicle {
void drive() { System.out.println("Driving vehicle"); }
}
class Car extends Vehicle {
void drive() { System.out.println("Driving car"); }
void honk() { System.out.println("Honk!"); }
}
public class Test {
public static void main(String[] args) {
Vehicle v = new Car(); // upcasting
v.drive();
// v.honk(); // would not compile
}
}Question 14
What is the output of this downcasting example?
class Animal {
void eat() { System.out.println("Eating"); }
}
class Dog extends Animal {
void bark() { System.out.println("Woof"); }
}
public class Test {
public static void main(String[] args) {
Animal a = new Dog();
Dog d = (Dog) a; // downcasting
d.bark();
}
}Question 15
Why is polymorphism important for frameworks and libraries?
Question 16
What is the difference between static and dynamic binding?
Question 17
Can private methods be overridden?
Question 18
What is the output of this polymorphic array example?
class Shape {
void draw() { System.out.println("Drawing shape"); }
}
class Circle extends Shape {
void draw() { System.out.println("Drawing circle"); }
}
class Square extends Shape {
void draw() { System.out.println("Drawing square"); }
}
public class Test {
public static void main(String[] args) {
Shape[] shapes = {new Circle(), new Square()};
for (Shape s : shapes) {
s.draw();
}
}
}Question 19
What happens with this invalid downcasting?
class Animal {}
class Dog extends Animal {}
class Cat extends Animal {}
public class Test {
public static void main(String[] args) {
Animal a = new Cat();
Dog d = (Dog) a; // Invalid downcast
}
}Question 20
Why is method overriding considered runtime polymorphism?
Question 21
What is the result of this method overloading with varargs?
class Printer {
void print(String s) { System.out.println("String: " + s); }
void print(String... s) { System.out.println("Varargs: " + s.length); }
}
public class Test {
public static void main(String[] args) {
Printer p = new Printer();
p.print("hello");
}
}Question 22
Can final methods be overridden?
Question 23
What is the output of this instanceof and downcasting example?
class Vehicle {}
class Car extends Vehicle {
void drive() { System.out.println("Driving"); }
}
public class Test {
public static void main(String[] args) {
Vehicle v = new Car();
if (v instanceof Car) {
Car c = (Car) v;
c.drive();
}
}
}Question 24
Why is polymorphism essential for the Strategy pattern?
Question 25
What is the result of this complex polymorphic hierarchy?
class A {
void method() { System.out.println("A"); }
}
class B extends A {
void method() { System.out.println("B"); }
}
class C extends B {
void method() { System.out.println("C"); }
}
public class Test {
public static void main(String[] args) {
A obj = new C();
obj.method();
}
}Question 26
Can constructors be overloaded?
Question 27
What is the difference between method hiding and method overriding?
Question 28
What is the output of this static method hiding example?
class Parent {
static void show() { System.out.println("Parent"); }
}
class Child extends Parent {
static void show() { System.out.println("Child"); }
}
public class Test {
public static void main(String[] args) {
Parent p = new Child();
p.show(); // Method hiding
}
}Question 29
Why does polymorphism support code reusability?
Question 30
What is the result of this covariant return type example?
class Animal {
Animal getParent() { return new Animal(); }
}
class Dog extends Animal {
Dog getParent() { return new Dog(); } // covariant return
}
public class Test {
public static void main(String[] args) {
Animal a = new Dog();
Dog d = (Dog) a.getParent();
System.out.println(d.getClass().getSimpleName());
}
}Question 31
Can overloaded methods have different access modifiers?
Question 32
What is the output of this polymorphic method call with super?
class Parent {
void show() { System.out.println("Parent"); }
}
class Child extends Parent {
void show() {
super.show();
System.out.println("Child");
}
}
public class Test {
public static void main(String[] args) {
Parent p = new Child();
p.show();
}
}Question 33
Why is polymorphism important for collections?
Question 34
What is the result of this method resolution example?
class MathUtils {
static int add(int a, int b) { return a + b; }
static double add(double a, double b) { return a + b; }
}
public class Test {
public static void main(String[] args) {
System.out.println(MathUtils.add(5, 3));
System.out.println(MathUtils.add(5.0, 3.0));
}
}Question 35
Mastering polymorphism means you now understand that powerful OOP code:
