0
0
Javaprogramming~20 mins

Upcasting and downcasting in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Casting Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Upcasting and Method Calls
What is the output of this Java code snippet?
Java
class Animal {
    void sound() {
        System.out.println("Animal sound");
    }
}
class Dog extends Animal {
    void sound() {
        System.out.println("Dog barks");
    }
    void fetch() {
        System.out.println("Dog fetches ball");
    }
}
public class Test {
    public static void main(String[] args) {
        Animal a = new Dog(); // upcasting
        a.sound();
    }
}
AAnimal sound
BDog fetches ball
CDog barks
DCompilation error
Attempts:
2 left
πŸ’‘ Hint
Remember that overridden methods use the actual object's method, even if referenced by a parent type.
❓ Predict Output
intermediate
2:00remaining
Downcasting and Method Access
What will be the output of this Java code?
Java
class Vehicle {
    void start() {
        System.out.println("Vehicle started");
    }
}
class Car extends Vehicle {
    void openSunroof() {
        System.out.println("Sunroof opened");
    }
}
public class Test {
    public static void main(String[] args) {
        Vehicle v = new Car(); // upcasting
        ((Car) v).openSunroof(); // downcasting
    }
}
ASunroof opened
BVehicle started
CClassCastException at runtime
DCompilation error
Attempts:
2 left
πŸ’‘ Hint
Downcasting allows access to subclass-specific methods.
❓ Predict Output
advanced
2:00remaining
Runtime Error from Invalid Downcasting
What error does this code produce when run?
Java
class Fruit {
    void eat() {
        System.out.println("Eating fruit");
    }
}
class Apple extends Fruit {
    void peel() {
        System.out.println("Peeling apple");
    }
}
class Orange extends Fruit {
    void peel() {
        System.out.println("Peeling orange");
    }
}
public class Test {
    public static void main(String[] args) {
        Fruit f = new Orange();
        Apple a = (Apple) f; // downcasting
        a.peel();
    }
}
AClassCastException at runtime
BPeeling orange
CPeeling apple
DCompilation error
Attempts:
2 left
πŸ’‘ Hint
Downcasting to a wrong subclass causes runtime errors.
🧠 Conceptual
advanced
2:00remaining
Understanding Upcasting Behavior
Given the code below, what will be the output when main runs?
Java
class Parent {
    void show() {
        System.out.println("Parent show");
    }
}
class Child extends Parent {
    void show() {
        System.out.println("Child show");
    }
    void play() {
        System.out.println("Child play");
    }
}
public class Test {
    public static void main(String[] args) {
        Parent p = new Child(); // upcasting
        p.show();
        // p.play(); // commented out
    }
}
AParent show
BChild show
CChild play
DCompilation error due to p.play()
Attempts:
2 left
πŸ’‘ Hint
Upcasting hides subclass-only methods but overridden methods run subclass versions.
❓ Predict Output
expert
3:00remaining
Output and Behavior of Complex Casting
What is the output of this Java program?
Java
class A {
    void m() {
        System.out.println("A.m()");
    }
}
class B extends A {
    void m() {
        System.out.println("B.m()");
    }
    void n() {
        System.out.println("B.n()");
    }
}
class C extends B {
    void m() {
        System.out.println("C.m()");
    }
    void n() {
        System.out.println("C.n()");
    }
}
public class Test {
    public static void main(String[] args) {
        A obj = new C(); // upcasting
        obj.m();
        ((B) obj).n(); // downcasting
    }
}
ACompilation error
BC.m()\nB.n()
CA.m()\nB.n()
DC.m()\nC.n()
Attempts:
2 left
πŸ’‘ Hint
Method calls use the actual object's version, even after casting.