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 Result
{
public static void main(String args[])
{
genericstackgenericstackObj0 = new genericstack ();
genericstackObj0.push("Inteview");
System.out.print(genericstackObj0.pop() + " ");
genericstackgenericstackObj1 = new genericstack ();
genericstackObj1.push("Mania");
System.out.println(genericstackObj1.pop());
}
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: D
Inerview Mania
- 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
- 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[])
{
genericstackgenericObject = new genericstack ();
genericObject.push(50);
System.out.println(genericObject.pop());
}
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
50
- Which of the following reference types cannot be generic?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
Anonymous inner class
- 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[])
{
genericstackgenericstackObj = new genericstack ();
genericstackObj.push("Interview Mania");
System.out.println(genericstackObj.pop());
}
}
-
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