Menu
Topics Index
...
`

Generics Wildcards2766


If you need explanation Read this topic

If you need Answer Take test on this topic

Generics Wildcards2766
What will be the output of the following program?
public class BoundedWildCardDemo {
    
    public static void showXY(Coordinates<?> c)
    {
        System.out.println("X Y Coordinates :");
                
        for(int i = 0; i < c.coords.length; i++)
        {
            System.out.println(c.coords[i].x+"    "+c.coords[i].y);
        }
    }
    public static void main(String arg[])
    {
        TwoD twoD[] = {
            new TwoD(1,1),new TwoD(7, 9), new TwoD(18, 4)
        };
        Coordinates<TwoD> twoDCoord =new Coordinates<TwoD>(twoD);
        showXY(twoDCoord);
    }
}
class TwoD
{
    int x, y;
    public TwoD(int x, int y) {
        this.x = x;
        this.y = y;
    }
}
class Coordinates<T extends TwoD>
{
    T[] coords;
    Coordinates(T[] coords)
    {
        this.coords = coords;
    }
}

A. X Y Coordinates :
1    1
1    1
1    1
B. X Y Coordinates :
1    1
7    9
18    4
C. X Y Coordinates :
D. Compilation Error 
E. 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 b. the parameter T occurs only once in the method signature, in an argument. You can imagine that the method body does not use the name T either. In this case you can use an alternative syntax, called wildcards, denoted with ?: wildcards represents unknowntype.                                                        the term generics means parameterized types. Parameterized types are important
because they enable you to create classes, interfaces, and methods in which the type of data
upon which they operate is specified as a parameter. Using generics, it is possible to create
a single class, for example, that automatically works with different types of data. A class,
interface, or method that operates on a parameterized type is called generic, as in generic class
or generic method..here generic with wildcard used so type parameter used here is unknown type.coming to this program TwoD class array is creatd and initialized.thn coordinates x,y values initialized with array value.                                   thn showXY()method called with coordinate values.                                                                     for loop prints x,y values

Posted by Maheshwari Natarajan    2014-11-26 18:25:28


This dose is now closed and the winners are Sai Ram,  for 'First Correct Comment', Maheshwari Natarajan,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Shashanka Mogaliraju. 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-27 08:03:17


© meritcampus 2019

All Rights Reserved.

Open In App