AlgoMaster Logo

Polymorphism - Quiz

Last Updated: January 3, 2026

Polymorphism Exercises

31 quizzes

1
Code Completion

Use a single polymorphic call to display details of the current Shape object.

java
1
shape.();

Click an option to fill the blank:

2
Code Completion

Invoke the correct overloaded method to calculate the total price using a double value.

java
1
double total = calculator.(19.99);

Click an option to fill the blank:

3
Code Completion

Create a polymorphic reference so that a ReportGenerator variable can refer to a PdfReportGenerator instance.

java
1
ReportGenerator generator = PdfReportGenerator();

Click an option to fill the blank:

4
Multiple Choice

In Java polymorphism, what does it mean to treat an object as an instance of its parent class?

5
Multiple Choice

Which statement best describes compile-time polymorphism in Java?

6
Multiple Choice

What is required for runtime polymorphism using method overriding?

7
Multiple Choice

Dynamic method dispatch decides which method implementation to call based on which factor?

8
Multiple Choice

Which of the following is an example of covariant return types?

9
Multiple Choice

Which scenario best benefits from runtime polymorphism in a real application?

10
Multiple Choice

In method overloading, which parts of the method definition must be different?

11
Multiple Choice

Which keyword helps prevent further overriding of a method, affecting polymorphism?

12
Multiple Choice

How does dynamic method dispatch interact with collections of a base type, like List<Shape>?

13
Multiple Choice

Which is TRUE about static methods and polymorphism?

14
Multiple Choice

Which code snippet correctly demonstrates a covariant return type in a subclass?

15
Sequencing

Order the steps to use runtime polymorphism for processing different Payment types (e.g., CardPayment, WalletPayment).

Drag and drop to reorder, or use the arrows.

16
Sequencing

Order the steps to implement and use covariant return types in a repository pattern.

Drag and drop to reorder, or use the arrows.

17
Output Prediction

What is the output of this code involving method overloading?

1class Printer {
2    void print(String s) { System.out.println("String: " + s); }
3    void print(Object o) { System.out.println("Object: " + o); }
4}
5public class Main {
6    public static void main(String[] args) {
7        Printer p = new Printer();
8        Object ref = "Hello";
9        p.print(ref);
10    }
11}
18
Output Prediction

What is the output of this runtime polymorphism example?

1class Notification {
2    String getType() { return "Generic"; }
3}
4class EmailNotification extends Notification {
5    String getType() { return "Email"; }
6}
7public class Main {
8    public static void main(String[] args) {
9        Notification n = new EmailNotification();
10        System.out.println(n.getType());
11    }
12}
19
Output Prediction

What will this code print when using covariant return types?

1class Report {
2    Report create() { return new Report(); }
3}
4class PdfReport extends Report {
5    @Override
6    PdfReport create() { return new PdfReport(); }
7}
8public class Main {
9    public static void main(String[] args) {
10        Report r = new PdfReport();
11        Report result = r.create();
12        System.out.println(result.getClass().getSimpleName());
13    }
14}
20
Output Prediction

What is the output of this code using dynamic dispatch on an array?

1class Shape {
2    String draw() { return "shape"; }
3}
4class Circle extends Shape {
5    String draw() { return "circle"; }
6}
7public class Main {
8    public static void main(String[] args) {
9        Shape[] shapes = { new Shape(), new Circle() };
10        System.out.println(shapes[1].draw());
11    }
12}
21
Output Prediction

What does this code print, illustrating overloaded and overridden methods?

1class Logger {
2    void log(Object o) { System.out.println("Object: " + o); }
3}
4class FileLogger extends Logger {
5    @Override
6    void log(Object o) { System.out.println("File: " + o); }
7    void log(String s, int level) { System.out.println("String: " + s + "@" + level); }
8}
9public class Main {
10    public static void main(String[] args) {
11        Logger logger = new FileLogger();
12        logger.log("event");
13    }
14}
22
Bug Spotting

This code tries to demonstrate runtime polymorphism with dynamic method dispatch for payments. Identify the bug that prevents overriding.

Click on the line(s) that contain the bug.

java
1
class Payment {
2
    static void process() {
3
        System.out.println("Generic payment");
4
    }
5
}
6
class CardPayment extends Payment {
7
    static void process() {
8
        System.out.println("Card payment");
9
    }
10
}
11
public class Main {
12
    public static void main(String[] args) {
13
        Payment p = new CardPayment();
14
        p.process();
15
    }
16
}
23
Bug Spotting

This code attempts to use a covariant return type in a subclass. Find the bug.

Click on the line(s) that contain the bug.

java
1
class Document {
2
    Document cloneDoc() {
3
        return new Document();
4
    }
5
}
6
class PdfDocument extends Document {
7
    @Override
8
    String cloneDoc() {
9
        return "pdf";
10
    }
11
}
12
 
24
Matching

Match each polymorphism-related term to its correct description.

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

25
Matching

Match each concept to its closest real-world example of polymorphic use.

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

26
Fill in the Blanks

Complete the code to demonstrate dynamic method dispatch for sending messages.

java
1
class MessageSender {
2
String send() { return "generic"; }
3
}
4
class SmsSender extends MessageSender {
5
@Override
6
String send() { return "sms"; }
7
}
8
public class Main {
9
public static void main(String[] args) {
10
sender = new SmsSender();
11
System.out.println(sender.());
12
}
13
}

Click an option to fill blank 1:

27
Fill in the Blanks

Fill in the blanks to correctly overload a method that calculates discounts.

java
1
class DiscountCalculator {
2
int apply(int amount) { return amount - 10; }
3
int apply( amount, rate) { return amount - (int)(amount * rate); }
4
}
5

Click an option to fill blank 1:

28
Fill in the Blanks

Complete the code to show covariant return types in a configuration loader.

java
1
class Config {
2
Config load() { return new Config(); }
3
}
4
class AppConfig extends Config {
5
@Override
6
load() { return new AppConfig(); }
7
}
8
public class Main {
9
public static void main(String[] args) {
10
Config c = new AppConfig().load();
11
System.out.println(c.getClass().());
12
}
13
}

Click an option to fill blank 1:

29
Fill in the Blanks

Fill in the blanks to use an overridden draw() method through a polymorphic reference.

java
1
abstract class Shape {
2
abstract void draw();
3
}
4
class Rectangle extends Shape {
5
@Override
6
void draw() { System.out.println("rectangle"); }
7
}
8
public class Main {
9
public static void main(String[] args) {
10
Shape shape = new ();
11
shape.();
12
}
13
}

Click an option to fill blank 1:

30
Hotspot Selection

Click the line where dynamic method dispatch decides which implementation of speak() to execute.

Click on the line to select.

java
1
class Animal {
2
    void speak() { System.out.println("Animal"); }
3
}
4
class Dog extends Animal {
5
    @Override
6
    void speak() { System.out.println("Dog"); }
7
}
8
public class Main {
9
    public static void main(String[] args) {
10
        Animal a = new Dog();
11
        a.speak();
12
    }
13
}
31
Hotspot Selection

Click the line that shows a covariant return type overriding a superclass method.

Click on the line to select.

java
1
class Connection {
2
    Connection open() { return new Connection(); }
3
}
4
class SecureConnection extends Connection {
5
    @Override
6
    SecureConnection open() { return new SecureConnection(); }
7
}
8