Generics
- What is the output of this program?
import java.util.*;
class genericstack
{
Stackobject = 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[])
{
genericstackgenericObj = new genericstack ();
genericObj.push("Interview");
System.out.println(genericObj.pop());
}
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: A
Interview
- Which of these type parameters is used for a generic class to return and accept a number?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: A
N is used for Number.
- Why are generics used?
-
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.
- Which of these type parameters is used for a generic class to return and accept any type of object?
-
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.
- Which of these Exception handlers cannot be type parameterized?
-
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