Menu
Topics Index
...
`

Generics Unbounded Wildcard2879


If you need explanation Read this topic

If you need Answer Take test on this topic

Generics Unbounded Wildcard2879
What will be the output of the following program?
public class GenericsWildCardDemo1 {
    public static void main(String args[])
    {
        Double[] ob =  {1.2, 2.3, 3.4, 4.5, 5.6};
        StoreNumbers<Double> obj = new StoreNumbers<Double>(ob);
        print(obj);
    }
    static void print(StoreNumbers<?> c)
    {
        for(int i = 0; i < c.obj.length; i++)
        {
            System.out.print(c.obj[i] + " ");
        }
    }
}

class StoreNumbers<T extends Number>
{
    T[] obj;
    StoreNumbers(T[] obj) {
        this.obj = obj;
    }
}

A. 1 2 3 4 5
B. 1.2 2.3 3.4 4.5 5.6 
C. Compilation Error
D. Runtime Error
Topic: Generics Wildcards In Java With Examples

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 B..here we have the generics wild card concept..wild cards are useful when we dont know the exact type while receving so we put the ? than it can receive the anytype of the data..we can have bounded wild cards also..here we have the unbounded wild card..

Execution:
here we have the class which can receive the objects of child classes of Number class..we create the object by passing the Double..and next we are calling the print() here it is using wildcard so it can receive any type of data..and next we are printing the data using the for loop so it will print  the data in the array as 1.2,2.3 etc

Posted by Uday Kumar    2015-01-28 15:19:03


ANS IS B 1.2,2.3,3.4,4.5,5.6                           Here the cocept of wildcard character "?" Is used.  ----It is used when we dont know what will be the data type of token   i.e. variable of array etc is unknown.  ---Here,storenumbers class is defined which extends numbers and one constructor in which value of object is initialized which is passed.   ---Here,double array is used to assign the object with value.  ---Then,print() method is called in which for loop is used to display each element by using array index.

Posted by Mânïshå Mùlchåndânï    2015-01-28 17:55:42


b.

Posted by Gaurav Bansal    2015-01-28 20:05:38


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Mânïshå Mùlchåndânï,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Gopinath Manchikanti. 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    2015-01-29 06:26:40


© meritcampus 2019

All Rights Reserved.

Open In App