Menu
Topics Index
...
`

Bounded Types2868


If you need explanation Read this topic

If you need Answer Take test on this topic

Bounded Types2868
What will be the output of the following program?
public class BoundedTypeDemo1 {
    public static void main(String args[])
    {
        BoundedType1<Integer> obj1 = new BoundedType1<Integer>();
        obj1.addSum(5);
        obj1.addSum(11);
        System.out.println("Value 2 :" + obj1.getSum());
        BoundedType1<Float> obj2 = new BoundedType1<Float>();
        obj2.addSum(new Float(11.5));
        obj2.addSum(new Float(18.5));
        System.out.println("Value 1 :" + obj2.getSum());
    }
}
class BoundedType1<T extends Number> {
    double sum = 0.0;
    void  addSum(T item)
    {
        sum+= item.doubleValue();
    }
    double getSum()
    {
        return sum;
    }
}

A. Value 1 :30.0
Value 2 :16.0
B. Value 2 :16.0
Value 1 :30.0
C. Compilation Error
D. Runtime Error
Topic: Java Bounded Type - Bounded Type 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.

b

Posted by Zeg Abder    2014-11-05 12:35:26


ans is B as obj1 is the object of the above class .obj1.addSum(5) goes to the method addSum and there sum=sum+item.doubleValue() here initially sum=0.0 item.doubleValue() Converts the value of the Number object to the double data type and returned it. here sum becomes sum=5.0 and then obj.addSum(11) here sum becomes 5.0+11.0  ie sum=16.0 it prints value2 :16.0         here for obj2 sum=11.5 in the first and in the next sum=11.5+18.5 ie sum=30.0 it prints Value1:30.0 and the output is Value 2: 16.0                                                                  Value 1: 30.0

Posted by Ramya Reddy Addulla    2014-11-05 12:46:01


b

Posted by Gaurav Bansal    2014-11-05 13:06:07


B

Posted by Vanaja Vannu    2014-11-05 15:18:32


Ans is B..here we  use the concept of Generics and in this we use use BoundedType, means restricting type...here T extends Number means T must be the child class of the Number...other wise compiler wil throw the error....

coming to main class we create Object for BoundedType class with Integer..here it is fin Bcz Integer is the Child class of Number...than we are calling the methods with 5 and 11..than we are adding these two numbers and converting it into double value and returning to main method than ot wil print as 16.0...

next we create an Float type Object and passing Float type objects...than here while adding it will perform auto unboxing than it will add than it wil b 30.0....

Posted by Uday Kumar    2014-11-05 15:51:18


ans b.in main bounded type integer object is created.by using that reference addsum() methods called.5,11 are passed as an argument.doublevalue() method is used to fetch an double value from integer object.this is called un boxing .integer class extends number class.number is base class it has many child classes integers, float,double etc.2st sop calls getsum() so this method returns 16.0 bcz we are doubleValue() method for unboxing the object. return primitive double value.similarly again addsum() called with float object here also doubleValue() method returns primitive float value of float object thn sop prints addition value 30.0.

Posted by Maheshwari Natarajan    2014-11-05 18:58:06


This dose is now closed and the winners are Ramya Reddy Addulla,  for 'First Correct Comment', Maheshwari Natarajan,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Bolo Raven. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2014-11-06 03:19:36


© meritcampus 2019

All Rights Reserved.

Open In App