Topics Index

Bounded Types2869

If you need explanation Read this topic

If you need Answer Take test on this topic

Bounded Types2869
What will be the output of the following program?
public class BoundedTypeDemo1 {
    public static void main(String args[])
        BoundedType1<Integer> obj1 = new BoundedType1<Integer>();
        System.out.println("Value 2 :" + obj1.getSum());
        BoundedType1<Float> obj2 = new BoundedType1<Float>();
        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 2 :16.0
Value 1 :25.0
B. Value 1 :16.0
Value 2 :25.0
C. Value 2 :16.0
Value 1 :0.0
D. Compilation Error
E. 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.

ans is C as the obj1 is object of boundedtype1 obj1.addSum (5) goes to the method and here sum=sum+item.doubleValue() in the item.doubleValue() converts the number into double here the sum becomessum=5.0and for the next obj1.addSum(11)   here sum=sum+item.doubleValue() here sum becomes sum=5.0+11.0=16.0 and in the next  sum value doesnt change it becomes sum=0.0 in the method getSum() it returns sum value  it prints    Value 2:16.0         value 1:0.0

Posted by Ramya Reddy Addulla    2014-11-06 04:47:30

ans c. here 1st integer object created and addsum() methods called with 5 and 11 args.nxt obj2 float object created but here also addsum method called with 1st object reference. 1st sop prints 16.0,2nd prints default double value 0.0 bcz this obj never used.doubleValue() method unboxing the integer object and returns the primitive double value

Posted by Maheshwari Natarajan    2014-11-06 04:51:57


Posted by Shubham Bansal    2014-11-06 07:19:05

answer main bounded type integer object is 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..nxt obj2 float object created.after that here again obj1 is used to call addsum() methods .here values 13,12 passed.1st sop prints 16.0.doublevalue() fetch double value from integer objct.thn 2nd sop prints 0.0 default primitive double value bcz obj2 is not used in this program ,it is just created.

Posted by NagaRaj Nataraj    2014-11-06 12:03:54

Ans is we have class named with BoundedType1 which is able to receive the Child classes of the Number...

here we create BoundedType1 object with Integer than memory will allocated for the sum initilized with the 0.0...
next we are calling the addSum() with Obj1 by passing the 5 than sum in Obj1 wil Bcm 5
doubleValue() will convert given Object into primitive this is called ""Unboxing""..

next we are calling addSum() with the 11..than we are adding these value to the sum which has already 5


now we are calling the getSum() with the Obj1 it wil return the value of sum from the Obj1

next we create the object for the BoundedType1 with Float memory will allocated for the sum


now we are calling the addSum() with Obj1 then in it addSum() 13 wil get added to the sum in Object1 Bcz we called with the Obj1...

next we are passing the 12 with Obj1 than sum wil Bcm 41.0...

now values..

now we are calling the getSum() with Obj2 so that it wil return 0.0

Posted by Uday Kumar    2014-11-06 14:08:45

This dose is now closed and the winners are Ramya Reddy Addulla,  for 'First Correct Comment', NagaRaj Nataraj, Uday Kumar,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Ramya Reddy Addulla. Please login into Merit Campus using facebook, to claim your recharge. Go to to raise the recharge.

Posted by Merit Campus    2014-11-07 04:17:07

© meritcampus 2019

All Rights Reserved.

Open In App