Menu
Topics Index
...
`

Generics2847


If you need explanation Read this topic

If you need Answer Take test on this topic

Generics2847
What will be the output of the following program?
public class GenericsWithObjectsDemo {
    public static void main(String args[]) {
        GenericsWithObjects<Double> doubleObject = new GenericsWithObjects<Double>(12.0);
        doubleObject.print();
        GenericsWithObjects<String> stringObject = new GenericsWithObjects<String>("MC");
        stringObject.print();
    }
}
class GenericsWithObjects<T> {
    Object obj;
    GenericsWithObjects(Object obj) {
        this.obj = obj;
    }
    void print() {
        System.out.println(obj);
    }
}

A. 12.0
MC
B. 12.0
12.0
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 A,, Object can hold both type values double and string

Posted by Raju VarshnEy    2014-11-13 08:43:57


ans is A  type parameters can represent only reference types, not primitive types (like int, double and char).

Posted by Sai Veerendra    2014-11-13 08:54:23


Ans is A...here we have class GenericWithObjects with parameter type t means that it can recevie any type of data....in java using generics we can create one method which will be worked for any type of data..we can have generic methods and classes..

here we are creating the instance for the class by passing the Double Object..and next we are calling the print() with this Object than it wil display the Content in it...

next  we are creating the instance for the class by passing the String Object..and next we are calling the print() with this Object than it wil display the Content in it...

Posted by Uday Kumar    2014-11-13 13:05:49


a

Posted by Shubham Bansal    2014-11-13 14:13:30


ans a.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. refernce for the double obj created.makes use of autoboxing to encapsulate the value 12.0, which is double, into an double.by using doubleobject reference print() called.sop prints 12.0 2nd string object is created with generic type class ,string dnt use any wrappers bcz string non primitive type.thn print() called with string obj reference MC get printd.

Posted by Maheshwari Natarajan    2014-11-13 17:35:53


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Maheshwari Natarajan,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Urvashi Arya. 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-14 02:45:49


© meritcampus 2019

All Rights Reserved.

Open In App