Topics Index


If you need explanation Read this topic

If you need Answer Take test on this topic

What will be the output of the following program?
public class GenericsWithObjectsDemo {
    public static void main(String args[])
        GenericsWithObjects<float> floatObject = new GenericsWithObjects<float>(33.0F);
class GenericsWithObjects<T>
    T obj;
    GenericsWithObjects(T obj)
        this.obj = obj;
    void print()

A. 33
B. Some other output
C. Compilation Error
D. Runtime Error
Topic: Java Generics

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 we have generics Concept which help ful to reduce the code...using this we can write single which will work for all type of Objects...and this also Type safe..

in Generics Evrything in the form of objects we have main class we are creating the Object for this class
Generics Totally based on the Compile time it will create Possible methods lik
fun(int x),fun(float x) etc...
here we are creating instance by passing the float(Primitive) here no autoboxing wil peroform..

here we use 33.0f means every floating pointg value treated as Double by that to say compiler treat it as float we are specifying f
when we are working with the Generics we must create Objects By passing the Objects Compiker will raise an error Saying Required Object..

Tak an Example When we are working with Collections we always use the Objects...if we want use int than we will create Integer...

if we change float  to Float  than it wil work fine

Posted by Uday Kumar    2014-11-12 03:50:43

Answer is C since Generic doent not support primitie types and we are specified a float primitive in the given program,it throws a compiler error

Posted by Shashanka Mogaliraju    2014-11-12 03:57:28


Posted by Priyanka Venkata    2014-11-12 05:29:01


Posted by Sandeep Bidhuri    2014-11-12 06:49:22

ans c.Compilation error.,in this program we have 2 classes,main class and one generic class.generics, which operates only on objects.With generics, you can define an algorithm once, independently of any specific
type of data, and then apply that algorithm to a wide variety of data types without any additional
effort.parameterized type class is called generic class. Here, T is the name of a type parameter. This name is used as a placeholder for the actual
type that will be passed to GenericTypesdiffDemo when an object is created. in this program we are trying to create generic class with primitive float value. It is important to understand that Java has always given you the ability to create generalized
classes, interfaces, and methods by operating through references of type Object. Because Object
is the superclass of all other classes, an Object reference can refer to any type object. When declaring an instance of a generic type, the type argument passed to the type parameter
must be a class type. You cannot use a primitive type, such as int or char or is possible to pass any class type to T, but you cannot pass a primitive type to a type floatobject declaration is illegal in this program.

Posted by Maheshwari Natarajan    2014-11-12 17:42:09

ans c.generic method's body is declared like that of any other method. Note that type parameters can represent only reference types, not primitive types

Posted by Sai Veerendra    2014-11-13 07:36:49


Posted by Shubham Bansal    2014-11-13 14:19:46

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

Posted by Merit Campus    2014-11-14 01:46:15

© meritcampus 2019

All Rights Reserved.

Open In App