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 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. Which of these type parameters is used for a generic class to return and accept a number?











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    N is used for Number.



  1. Why are generics used?











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    Generics add stability to your code by making more of your bugs detectable at compile time.


  1. Which of these type parameters is used for a generic class to return and accept any type of object?











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: B

    T is used for type, A type variable can be any non-primitive type you specify: any class type, any interface type, any array type, or even another type variable.



  1. Which of these Exception handlers cannot be type parameterized?











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: D

    we cannot Create, Catch, or Throw Objects of Parameterized Types as generic class cannot extend the Throwable class directly or indirectly