Topics Index

instance of output

If you need explanation Read this topic

If you need Answer Take test on this topic

instance of output
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 {}

D. Some other output not shown here
E. Compilation Error or Run-time Error
Topic: Advanced Java Programming Concepts

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

ans:'A': because of sequence where ever implements is given it creates an instance but does not instantiate so it doesn't print.

Posted by Karthik Narahari    2014-08-20 12:35:16

ans :e
interface cant implements another inteface
cant create obj for interface

Posted by Ananda Kumar    2014-08-20 12:37:47

ans d .
it prints some othr

Posted by Maheshwari Natarajan    2014-08-20 13:09:31

ans : D which is U UV UV UWYZ UWYZ UWYZ UVWX....instanceof operator is used to test whether the object is an instance of the specified type (class or subclass or interface).

Posted by Shashanka Mogaliraju    2014-08-20 13:25:53

ans is A because interfaces can be referenced cannot be instantiated

Posted by Ramya Reddy Addulla    2014-08-20 13:34:09


Posted by Prabhakar Reddy    2014-08-20 13:57:26

Congratulations Shashanka Mogaliraju. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-08-21 05:16:59

© meritcampus 2019

All Rights Reserved.

Open In App