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[])
{
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
- 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(100);
System.out.println(genericObject.pop());
}
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: E
100
- What is the output of this program?
import java.util.*;
public class Result
{
public static double sumOfList(List extends Number> list)
{
double var = 0.0;
for (Number num : list)
var += num.doubleValue();
return var;
}
public static void main(String args[])
{
ListListObject = Arrays.asList(10, 20, 30);
System.out.println(sumOfList(ListObject));
}
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: E
None of these
- Which of the following cannot be Type parameterized?
-
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.
- What are generic methods?
-
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.