Java Polymorphism (Compile-Time & Runtime) Quiz

Java
0 Passed
0% acceptance

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.

35 Questions
~70 minutes
1

Question 1

What is polymorphism in object-oriented programming?

A
The ability of objects to take on many forms and respond differently to the same method call
B
The ability to create multiple classes with the same name
C
The ability to hide data from external access
D
The ability to inherit from multiple classes
2

Question 2

What is the difference between compile-time and runtime polymorphism?

A
Compile-time is method overloading, runtime is method overriding
B
Compile-time happens during execution, runtime during compilation
C
They are the same thing
D
Runtime polymorphism is faster
3

Question 3

What is method overloading?

A
Defining multiple methods with the same name but different parameter lists in the same class
B
Overriding a method from a superclass
C
Creating methods with the same signature in different classes
D
Using the same method name in interfaces
4

Question 4

What is method overriding?

A
Providing a specific implementation of a method in a subclass that is already defined in its superclass
B
Defining multiple methods with the same name in the same class
C
Creating abstract methods
D
Using final methods
5

Question 5

What is dynamic method dispatch?

A
The mechanism by which the JVM determines which overridden method to call at runtime based on the actual object type
B
How the compiler resolves overloaded methods
C
The process of loading classes at runtime
D
How interfaces are implemented
6

Question 6

What is upcasting?

A
Casting a subclass reference to a superclass reference, which is always safe
B
Casting a superclass reference to a subclass reference
C
Converting primitive types
D
Creating new objects
7

Question 7

What is downcasting?

A
Casting a superclass reference to a subclass reference, which requires explicit casting and can throw ClassCastException
B
Casting a subclass to a superclass
C
Automatic type conversion
D
Converting between primitive types
8

Question 8

Why does polymorphism matter in software design?

A
It enables writing flexible code that works with different types through a common interface
B
It makes code run faster
C
It reduces memory usage
D
It eliminates the need for inheritance
9

Question 9

What is the output of this method overloading example?

java
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));
    }
}
A
8 8.0
B
8.0 8.0
C
Compilation error
D
Runtime exception
10

Question 10

What is printed by this runtime polymorphism example?

java
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();
    }
}
A
Woof
B
Animal sound
C
Compilation error
D
Runtime exception
11

Question 11

Can overloaded methods have different return types?

A
Yes, as long as the parameter lists are different
B
No, return type alone is not sufficient
C
Only if they are in different classes
D
Only for primitive types
12

Question 12

What happens when you try to override a method with a different return type?

A
It's allowed if the return type is covariant (subclass of the original return type)
B
It's always allowed
C
It causes a compilation error
D
It creates method overloading
13

Question 13

What is the result of this upcasting example?

java
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
    }
}
A
Driving car
B
Driving vehicle
C
Compilation error
D
Runtime exception
14

Question 14

What is the output of this downcasting example?

java
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();
    }
}
A
Woof
B
Eating
C
ClassCastException
D
Compilation error
15

Question 15

Why is polymorphism important for frameworks and libraries?

A
It allows frameworks to work with user-defined classes through common interfaces
B
It makes frameworks faster
C
It reduces framework size
D
It eliminates the need for interfaces
16

Question 16

What is the difference between static and dynamic binding?

A
Static binding occurs at compile-time for overloaded methods, dynamic binding at runtime for overridden methods
B
Static binding is for variables, dynamic for methods
C
They are the same
D
Dynamic binding is faster
17

Question 17

Can private methods be overridden?

A
No, private methods are not inherited and cannot be overridden
B
Yes, but only in the same class
C
Yes, with public access
D
Only static private methods
18

Question 18

What is the output of this polymorphic array example?

java
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();
        }
    }
}
A
Drawing circle Drawing square
B
Drawing shape Drawing shape
C
Compilation error
D
Runtime exception
19

Question 19

What happens with this invalid downcasting?

java
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
    }
}
A
ClassCastException at runtime
B
Compiles and runs fine
C
Compilation error
D
NullPointerException
20

Question 20

Why is method overriding considered runtime polymorphism?

A
The actual method called is determined at runtime based on the object's type, not the reference type
B
It happens during compilation
C
It only works with static methods
D
It requires explicit casting
21

Question 21

What is the result of this method overloading with varargs?

java
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");
    }
}
A
String: hello
B
Varargs: 1
C
Compilation error
D
Ambiguous method call
22

Question 22

Can final methods be overridden?

A
No, final methods cannot be overridden to prevent changing their behavior
B
Yes, but only in subclasses
C
Only static final methods
D
Only private final methods
23

Question 23

What is the output of this instanceof and downcasting example?

java
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();
        }
    }
}
A
Driving
B
Compilation error
C
ClassCastException
D
No output
24

Question 24

Why is polymorphism essential for the Strategy pattern?

A
It allows switching between different algorithms at runtime through a common interface
B
It makes algorithms faster
C
It reduces code size
D
It eliminates the need for interfaces
25

Question 25

What is the result of this complex polymorphic hierarchy?

java
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();
    }
}
A
C
B
B
C
A
D
Compilation error
26

Question 26

Can constructors be overloaded?

A
Yes, constructors can be overloaded like any other method
B
No, each class can have only one constructor
C
Only in abstract classes
D
Only with different return types
27

Question 27

What is the difference between method hiding and method overriding?

A
Overriding is for instance methods (runtime polymorphism), hiding is for static methods (compile-time resolution)
B
They are the same
C
Hiding is for private methods
D
Overriding requires final methods
28

Question 28

What is the output of this static method hiding example?

java
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
    }
}
A
Parent
B
Child
C
Compilation error
D
Runtime exception
29

Question 29

Why does polymorphism support code reusability?

A
It allows writing generic code that works with future subclasses without modification
B
It makes code smaller
C
It eliminates inheritance
D
It requires more classes
30

Question 30

What is the result of this covariant return type example?

java
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());
    }
}
A
Dog
B
Animal
C
Compilation error
D
ClassCastException
31

Question 31

Can overloaded methods have different access modifiers?

A
Yes, access modifiers don't affect overloading since it's based on parameter types
B
No, they must have the same access modifier
C
Only public and private
D
Only in interfaces
32

Question 32

What is the output of this polymorphic method call with super?

java
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();
    }
}
A
Parent Child
B
Child Parent
C
Parent
D
Compilation error
33

Question 33

Why is polymorphism important for collections?

A
It allows storing different object types in the same collection through common interfaces
B
It makes collections faster
C
It reduces collection size
D
It eliminates generics
34

Question 34

What is the result of this method resolution example?

java
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));
    }
}
A
8 8.0
B
8.0 8.0
C
Compilation error
D
Ambiguous call
35

Question 35

Mastering polymorphism means you now understand that powerful OOP code:

A
Uses compile-time overloading for convenience and runtime overriding for flexibility, with safe casting enabling polymorphic collections and extensible architectures
B
Avoids inheritance entirely
C
Makes everything static
D
Prioritizes speed over design

QUIZZES IN Java