Menu
Topics Index
...
`

Classes inside other class1990


If you need explanation Read this topic

If you need Answer Take test on this topic

Classes inside other class1990
What will be the output of the following program?
public class XYZ {
    public static void main(String[] args) {
        System.out.println(X.Y.Z);
    }
}
class X {
    static class Y {
        static String Z = "Black";
    }
    static C Y = new C();
}
class C {
    static String Z = "White";
}

A. White
B. Black
C. BlackWhite
D. WhiteBlack
E. Some other output
F. Compilation Error or Runtime Error
Topic: Calling A Class From Another Class In Java

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 is A...x.y refers to a static class...calling this will execute the static block hence after assigning black to z...Class C object is created and white is assigned to it./..so Z contains White

Posted by Santosh Munugota    2013-12-04 08:48:45


answer is 'F' because variable Z cannot be referenced in main so we get compilation error in println statement

Posted by Sai Teja    2013-12-04 08:55:21


ans is A,we get the output as White.

Posted by Ankit Mudiraj    2013-12-04 13:41:24


ans is A bcoz simple name may occur in contexts where it may potentially be interpreted as the name of a variable, a type, or a package. In these situations, the rules specify that a variable will be chosen in preference to a type(class), and that a type (class) will be chosen in preference to a package. so in dis case the variable Y is given higher prefernce when compared to class Y. so X.Y.Z gives White

Posted by Pramod Jain    2013-12-04 15:32:49


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

Posted by Merit Campus    2013-12-05 03:32:13


© meritcampus 2019

All Rights Reserved.

Open In App