Which line inserted independtly at line 1 will allow to compile?
a)c.add(new Object());
b)c.add("Java");
c)c.add(null);
d)All 3 lines above inserted independtely will allow to compile
e)All 3 lines above inserted independtely will not allow to compile
Answer c)
a)-->we don’t know what the element type c will be,So we cannot add Object to it.
b)-->we don’t know what the element type c will be,So we cannot add String to it.
c)-->null is allowed because it is member of every type.
d)-->Because of option a & b, d also wrong.
e)-->Because of option c, e also wrong.
2) Which line(s) is free from compile time error?
Assume that Parent is a Base class and Child is a Derived class
List alList = new ArrayList(); //line 1 List alList = new ArrayList(); //line 2 List alList = new ArrayList(); //line 3 List alList = new ArrayList(); //line 4 List alList = new ArrayList(); //line 5
a)only 1
b)only 4
c)only 5
d)2 and 3
e)1 and 4
Answer c)
a)-->Reference can hold only Parent class and its super types.
b)-->Reference can hold only Child class and its sub types.
c)-->Wild card types are applicable for references and it can accept anytype parameter object assign to it
d)-->We cannot use wildcard notation in the object creation.
e)-->line 1 failed because Reference can hold only Parent class & its super types and line 2 failed because Reference can hold only Child class & its sub types.
3) public static void addandDisp(Collection cs, T t) { for (T o : cs) { s.add(o); } for (T o : cs) { System.out.println(o); } }
No comments:
Post a Comment