Menu
Topics Index
...
`

Generics2786


If you need explanation Read this topic

If you need Answer Take test on this topic

Generics2786
What will be the output of the following program?
import java.util.*;
public class SampleDemo {
    public static void main(String args[]) {
        Sample <Integer> obj = new Sample<Integer>();
        obj.push(36);
        System.out.println(obj.pop());
    }
}
class Sample<E>
{
    Stack <E> ob = new Stack <E>();
    public void push(E obj) {
        ob.push(obj);
    }
    public E pop() {
        E obj = ob.pop();
        return obj;
    }
}

A. 36
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.

Answer is A ....36....Here sample is a generic class i.e..  
Java Generic methods and generic classes enable programmers to specify, with a single method declaration, a set of related methods or, with a single class declaration, a set of related types, respectively.
Generics also pr ovide compile-time type safety that allows programmers to catch invalid types at compile time. ...We have an instance of stack created and this stack also accepts a Generic Type ...obj.push(36) from main will invoke push method in Sample class where it is actuallly pushed into the stack that we created inside the sample class...Stack is a data structure which enforces Lat-In-First-Out policy..obj.pop will invoke pop method of sample class which will actually pop the item from the top of the stack which is integer 36 in this example and this is returned back to main program and printed on the console

Posted by Shashanka Mogaliraju    2014-11-13 07:01:30


This dose is now closed and the winners are Shashanka Mogaliraju,  for 'First Correct Comment', Shashanka Mogaliraju,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Shubham Bansal. 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:35:16


© meritcampus 2019

All Rights Reserved.

Open In App