Menu
Topics Index
...
`

Creating objects for class1782


If you need explanation Read this topic

If you need Answer Take test on this topic

Creating objects for class1782
What will be the output of the following program?
public class Island {
    static String i = "hai";
    public static void main(String[] args) {
        Island i2 = new Island();
        Island i3 = new Island();
        Island i4 = new Island();
        i2.i = "hello";
        i3.i = "good";
        i4.i = "evening";
        System.out.print(i2.i+"-");
        System.out.print(i3.i+"-");
        System.out.print(i4.i+"-");
        System.out.println(Island.i);
        i2 = null;
        i3 = null;
        i4 = null;
        System.out.print(i2+"-");
        System.out.print(i3+"-");
        System.out.print(i4);
    }
}

A. hai-hai-hai-hai
null-null-null
B. hello-good-evening-hai
null-null-null
C. evening-evening-evening-evening
null-null-null
D. Some other output
E. Compilation Error or Runtime Error
Topic: Static and Non Static Variables - Static and Non Static Methods

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 c...as i is a static variable...the value of i before print statement is evening...then if we assign null to Island object then it prints null

Posted by Santosh Munugota    2013-09-18 08:29:59


Ans is C.. static variables are common for all the instances of the class..so the last modification is in memory (i4.i="evening")..so evening-evening-evening-evening is printed and i2,i3,i4 are assigned null so null-null-null is printed

Posted by Krishna Sai    2013-09-18 08:30:01


C is the answer.as in first three SOP i2.i ,i3.iand i4.i gives "evening" as its the last updated string. next three SOP respective i2 i3 and i4 values are printed.

Posted by Shaik Shayista    2013-09-18 08:35:49


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

Posted by Merit Campus    2013-09-19 03:29:26


© meritcampus 2019

All Rights Reserved.

Open In App