0
0
Javaprogramming~20 mins

Runtime polymorphism in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Runtime Polymorphism Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of overridden method call

What is the output of this Java program demonstrating runtime polymorphism?

Java
class Animal {
    void sound() {
        System.out.println("Animal makes a sound");
    }
}

class Dog extends Animal {
    @Override
    void sound() {
        System.out.println("Dog barks");
    }
}

public class Test {
    public static void main(String[] args) {
        Animal a = new Dog();
        a.sound();
    }
}
ADog barks
BAnimal makes a sound
CCompilation error
DRuntime error
Attempts:
2 left
πŸ’‘ Hint

Remember that the method called depends on the actual object type, not the reference type.

❓ Predict Output
intermediate
2:00remaining
Output with multiple overridden methods

What will be printed when this Java code runs?

Java
class Vehicle {
    void start() {
        System.out.println("Vehicle starts");
    }
}

class Car extends Vehicle {
    @Override
    void start() {
        System.out.println("Car starts");
    }
}

class SportsCar extends Car {
    @Override
    void start() {
        System.out.println("SportsCar starts quickly");
    }
}

public class Test {
    public static void main(String[] args) {
        Vehicle v = new SportsCar();
        v.start();
    }
}
AVehicle starts
BCar starts
CCompilation error
DSportsCar starts quickly
Attempts:
2 left
πŸ’‘ Hint

Which class's start() method is called depends on the actual object type.

❓ Predict Output
advanced
2:00remaining
Output with casting and overridden methods

What is the output of this Java program?

Java
class Parent {
    void show() {
        System.out.println("Parent show");
    }
}

class Child extends Parent {
    @Override
    void show() {
        System.out.println("Child show");
    }
    void childOnly() {
        System.out.println("Child only method");
    }
}

public class Test {
    public static void main(String[] args) {
        Parent p = new Child();
        p.show();
        ((Child) p).childOnly();
    }
}
A
Child show
Compilation error
BRuntime error
C
Child show
Child only method
D
Parent show
Child only method
Attempts:
2 left
πŸ’‘ Hint

Check which method is overridden and which method is only in the child class.

❓ Predict Output
advanced
2:00remaining
Output with overridden method and variable hiding

What will this Java program print?

Java
class Base {
    String name = "Base";
    void printName() {
        System.out.println(name);
    }
}

class Derived extends Base {
    String name = "Derived";
    @Override
    void printName() {
        System.out.println(name);
    }
}

public class Test {
    public static void main(String[] args) {
        Base b = new Derived();
        System.out.println(b.name);
        b.printName();
    }
}
A
Derived
Derived
B
Base
Derived
C
Base
Base
D
Derived
Base
Attempts:
2 left
πŸ’‘ Hint

Remember that variables are not polymorphic, but methods are.

❓ Predict Output
expert
3:00remaining
Output with abstract class and runtime polymorphism

What is the output of this Java program using an abstract class and runtime polymorphism?

Java
abstract class Shape {
    abstract void draw();
}

class Circle extends Shape {
    @Override
    void draw() {
        System.out.println("Drawing Circle");
    }
}

class Square extends Shape {
    @Override
    void draw() {
        System.out.println("Drawing Square");
    }
}

public class Test {
    public static void main(String[] args) {
        Shape s = new Circle();
        s.draw();
        s = new Square();
        s.draw();
    }
}
A
Drawing Circle
Drawing Square
B
Drawing Square
Drawing Circle
CRuntime error
DCompilation error: cannot instantiate abstract class
Attempts:
2 left
πŸ’‘ Hint

Abstract classes cannot be instantiated directly, but references can point to subclass objects.