Menu
Topics Index
...
`

Wrapper Classes Comparison


If you need explanation Read this topic

If you need Answer Take test on this topic

Wrapper Classes Comparison
What will be the output of the following program?
public class DataTypes {
    public static void main(String[] args) {
        int result = 0;
        short s = 42;
        Long x = new Long("42");
        Long y = new Long(42);
        Short z = new Short("42");
        Short x2 = new Short(s);
        Integer y2 = new Integer("42");
        Integer z2 = new Integer(42);
        if (x == y)    result = 1;
        if (x.equals(y))    result = result + 10;
        if (x.equals(z))    result = result + 100;
        if (x.equals(x2))    result = result + 1000;
        if (x.equals(z2))    result = result + 10000;
        System.out.println("result = " + result);
    }
}

A. result = 0
B. result = 1
C. result = 10
D. result = 11
E. result = 11010
F. Compilation Error or Runtime Error
Topic: Creating Objects for Primitive Data Types (Byte, Short)

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 only if(x.equals(y)) is satisfied n result=10+0 ..so result=10;

Posted by Krishna Sai    2013-06-07 12:39:58


ans is c...cuz the equals method gives true only if they belong to same class i.e,long here...so only 1 if is executed and result value is 10

Posted by Santosh Munugota    2013-06-07 12:40:08


option C: if an object exists with the existing value then on creation of new object the new reference points to new object so the references dont bcum equal . also as it cannot become equal in case of objects of two different classes answer would be 10 because  for equals function to be true both type of object and value in it must be same which is same only in case of x and y.

Posted by Ratan Dhoka    2013-06-07 12:44:33


Ans i c

Posted by Sekhar Dutt    2013-06-07 12:45:47


Answer is C : since here x and y both are created wit same object and have same values ! therefore the 2nd if condition is satisified which is incremented to +10 !! so the o/p will be result =10.

Posted by Rehana Tabassum    2013-06-07 13:05:20


Congratulations Krishna Sai Mulpuri. You are today's daily dose winner. Please send us your mobile details and we will process the recharge asap.

Posted by Merit Campus    2013-06-07 15:27:21


received rc !! thanks :)

Posted by Krishna Sai    2013-06-10 15:25:12


© meritcampus 2019

All Rights Reserved.

Open In App