Generics


  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 Result
    {
    public static void main(String args[])
    {
    genericstack genericstackObj0 = new genericstack();
    genericstackObj0.push("Inteview");
    System.out.print(genericstackObj0.pop() + " ");
    genericstack genericstackObj1 = new genericstack();
    genericstackObj1.push("Mania");
    System.out.println(genericstackObj1.pop());
    }
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: D

    Inerview Mania


  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 genericObj = new genericstack();
    genericObj.push("Interview");
    System.out.println(genericObj.pop());
    }
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    Interview



  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 genericObject = new genericstack();
    genericObject.push(50);
    System.out.println(genericObject.pop());
    }
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    50


  1. Which of the following reference types cannot be generic?











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    Anonymous inner class



  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. View Hint View Answer Discuss in Forum

    NA

    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