Menu
Topics Index
...
`

Type Match


If you need explanation Read this topic

If you need Answer Take test on this topic

Type Match
What will be the output of the following program?
public class GenericsDemo {
    public static void main(String[] args) {
        A b = new A(20);
        int sum = 10;
        sum += b.ob;
        System.out.println("Sum = " + sum);
    }
}
class A<T> {
    T ob;
    public A(T ob) {
        this.ob = ob;
    }
    public void print(T ob) {
        System.out.println("Obj = " + ob);
    }
}

A. Obj = 20
Sum = 30
B. Obj = 10
Sum = 10
C. Obj = 10
Sum = 30
D. Compilation Error or Runtime Error
Topic: Java Type Erasure

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.

a.obj b holds the value 20.sum=$um b.ob..hrnce sum=30..

Posted by Asma Mujtaba Khan    2014-08-21 03:59:18


d.bad operandtypes for binary operator "+"...
sum+=b.ob

sum is int n b.ob is obj..
also non stativ membrs cannot be refrn by static cntxt

Posted by Nasir Khan    2014-08-21 04:20:07


Ans is D.It will throw a compile time error since the compiler cannot resolve 'T' at compile time and hence + operator seems to be invalid between an int and generic ibject .Hence it does not allow such unsafe operations

Posted by Shashanka Mogaliraju    2014-08-21 08:07:16


primitive data type can not accept the generic data type operations

Posted by Din Kurth    2014-08-21 17:28:44


Congratulations Nasir Khan. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-08-22 05:15:36


© meritcampus 2019

All Rights Reserved.

Open In App