Home » JAVA Programming » Generics » Question
  1. What is the output of this program?
    import java.util.*;
    class genericstack
    {
    Stack object = new Stack ();
    public void push(E obj)
    {
    object.push(obj);
    }
    public E pop()
    {
    E obj = object.pop();
    return obj;
    }
    }

    public class Output
    {
    public static void main(String args[])
    {
    genericstack genericstackObj = new genericstack();
    genericstackObj.push("Interview Mania");
    System.out.println(genericstackObj.pop());
    }
    }
    1. Interview Mania
    2. Mania
    3. Interviiw
    4. Compilation Error
    5. Runtime Error
Correct Option: D

genericstack’s object genericstackObj is defined to contain a integer parameter but we are sending an string parameter, which results in compilation error.

utput.java:21: error: incompatible types: String cannot be converted to Integer
genericstackObj.push("Interview Mania");
^
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
1 error



Your comments will be displayed only after manual approval.