Menu
Question Index
...

What will be the output of the following program?

class PrintClassName {
    public static void main(String[] args) {
        print(new U());
        print(new V());
        print((U)new V());
        print(new Z());
        print((U)new Z());
        print((Y)new Z());
        print((W)new X());
    }

    public static void print(Object o) {
        System.out.print((o instanceof U) ? "U" : "");
        System.out.print((o instanceof V) ? "V" : "");
        System.out.print((o instanceof W) ? "W" : "");
        System.out.print((o instanceof X) ? "X" : "");
        System.out.print((o instanceof Y) ? "Y" : "");
        System.out.print((o instanceof Z) ? "Z" : "");
        System.out.print(" ");
    }
}

class U {}
class V extends U {}
interface W {}
class X extends V implements W {}
interface Y extends W {}
class Z extends U implements Y {}


U UV UV UVWYZ UVWYZ UVWYZ UVWX
U UV UV UYZ UYZ UYZ UVWX
U UV UV UWYZ UWYZ UWYZ WX
Some other output not shown here
Compilation Error or Run-time Error

Doubts

Problems

Topic: Advanced Java Programming Concepts

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App