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 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


  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(100);
    System.out.println(genericObject.pop());
    }
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: E

    100



  1. What is the output of this program?
    import java.util.*;
    public class Result
    {
    public static double sumOfList(List list)
    {
    double var = 0.0;
    for (Number num : list)
    var += num.doubleValue();
    return var;
    }
    public static void main(String args[])
    {
    List ListObject = Arrays.asList(10, 20, 30);
    System.out.println(sumOfList(ListObject));
    }
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: E

    None of these


  1. Which of the following cannot be Type parameterized?











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    Cannot Overload a Method Where the Formal Parameter Types of Each Overload Erase to the Same Raw Type.



  1. What are generic methods?











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    Generic methods are methods that introduce their own type parameters. This is similar to declaring a generic type, but the type parameter scope is limited to the method where it is declared. Static and non-static generic methods are allowed, as well as generic class constructors.